php foreach与嵌入的html抛出错误

时间:2014-02-03 04:08:04

标签: php html mysql pdo

我试图使用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子句或某事)。

2 个答案:

答案 0 :(得分:2)

<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>

答案 1 :(得分:0)

在你有另一个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>