【问题标题】:php foreach with inlayed html throwing errors带有嵌入html的php foreach抛出错误
【发布时间】:2014-02-03 04:08:03
【问题描述】:

我正在尝试使用 PDO 从 mysql 中获取数据并将其输出到 html 表中

  <div class="data">
    <table class="table table-striped">
      <?php 
        if($_POST['submit'] && $_POST['search_for'] == "keyword") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
        } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
        } else {
          foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
        }
      ?>
        <tr>
          <td width="10%"><?php echo $row['address'] ?></td>
          <td><?php echo $row['log'] ?></td>
          <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
        </tr>
      <?php endforeach; ?>
    </table>
  </div>

我在 elseif 声明之前只说意外的 '}' 时收到一个错误。

虽然我用的时候

  <div class="data">
    <table class="table table-striped">
      <?php 
          foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
      ?>
        <tr>
          <td width="10%"><?php echo $row['address'] ?></td>
          <td><?php echo $row['log'] ?></td>
          <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
        </tr>
      <?php endforeach; ?>
    </table>
  </div>

没有发生错误,代码正常运行,从 mysql 数据库中获取数据并将其显示为 html 表。

我本质上是想根据发送到页面的发布数据来更改表格的输出。

有没有一种方法可以用类似于后面的 if 语句格式化第一个代码,或者我应该在每个 if 语句循环时回显 html(我假设发生错误是因为它正在关闭 foreach 子句什么的)。

【问题讨论】:

    标签: php html mysql pdo


    【解决方案1】:
    <div class="data">
        <table class="table table-striped">
          <?php 
            if($_POST['submit'] && $_POST['search_for'] == "keyword") {
              $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
              $rows = $stmt->execute($_POST['search_for'];
            } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
              $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
              $rows = $stmt->execute($_POST['search_for'];
            } else {
              $rows = $db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') ;             
            }
            foreach($rows as $row): 
          ?>
            <tr>
              <td width="10%"><?php echo $row['address'] ?></td>
              <td><?php echo $row['log'] ?></td>
              <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
            </tr>
          <?php endforeach; ?>
        </table>
      </div>
    

    【讨论】:

    • 谢谢。没想到像那样把foreach放在最后。
    【解决方案2】:

    在您有另一个foreach 语句之前,您应该结束第一个。

    所以它看起来像这样。

    <div class="data">
    <table class="table table-striped">
      <?php 
        if($_POST['submit'] && $_POST['search_for'] == "keyword") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
          endforeach;
        } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
          //No code here?
          endforeach;
        } else {
          foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
        }
      ?>
        <tr>
          <td width="10%"><?php echo $row['address'] ?></td>
          <td><?php echo $row['log'] ?></td>
          <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
        </tr>
      <?php endforeach; ?>
    </table>
    

    【讨论】:

    • 我也很好奇这里使用了什么数据库抽象层,因为我熟悉的任何数据库抽象层都不会返回 query 上的结果集,而是返回资源 ID,其中您仍然需要执行获取,以指示您希望返回的结果集的类型,即关联数组、对象等...
    • 我认为他实际上想要做的是使用底部代码作为他从顶部开始的foreach 的主体。
    • @Ohgodwhy 我认为它是 PDO。它的语句对象实现了Iterable
    • @Barmar 是的,这就是我想要做的。我希望我可以基于条件语句启动一个 foreach,然后其余的会失败,然后我可以输出我的表数据并在完成后结束它。
    猜你喜欢
    • 1970-01-01
    • 2013-12-18
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2015-06-29
    相关资源
    最近更新 更多