php foreach with inlayed html throwing errors

I am attempting to grab data from mysql using PDO and output it into a html table like so

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

I get an error just saying unexpected '}' right before the elseif declaration.

Although when I use

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

No errors occurs and the code works as it should, grabbing data from the mysql database and displaying it a html table.

I essentially want to change the output of the table based on post data sent to the page.

Is there a way I can format the first code with the if statements similarly to the later, or should I just echo out the html in each if statement as it loops (I assume the error occurs because it is closing the foreach clause or something).

Answers


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

Before you have another foreach statement you should end the first one.

So it would look something like this.

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


Need Your Help

PHP: Classes to use functions from each other?

php class dependency-injection

I've been into this problem for a while already, and have asked some questions about it here Stackoverflow. I've got some advice, but I just can't understand it. Could someone provide me an example...