正确形成多个foreach语句

时间:2019-03-29 04:05:53

标签: php

在我的编码中,我有两个foreach语句。但是我编写代码的方式是错误的。谁能告诉我编写多个foreach语句的正确格式是什么。

用于标识id,passport_is,blood_group,blood_bag的$ people语句。 捐赠日期的$ donor声明。

提前谢谢。

这是我的代码

     <?php 
$db = new PDO('mysql:dbname=mypro_bms;host=localhost', 'root', '');
if (isset($_GET['q'])) {
  $q = $_GET['q'];
  $statement = $db->prepare("select * from donate where passport_IC like :passport_IC");
  $statement->execute([
    ':passport_IC' => '%' . $q .'%'

  ]);

} else {
  $statement = $db->prepare('select * from donate');
  $statement->execute();
}
$people = $statement->fetchAll(PDO::FETCH_OBJ);
 ?>
<?php
$statement = $db->prepare('select donation_date from tbl_user');
$statement->execute();
$donor = $statement->fetchAll(PDO::FETCH_OBJ);
?>
<table class="table table-bordered">
          <tr>
            <th>id</th>
            <th>Passport/IC</th>
            <th>Blood Group</th>
            <th>Blood Bag Type</th>


<?php foreach($peoples as $people){
     foreach($donors as $donor){ ?>
      <tr>
               <td><center><b><font color="black"><?php echo $people->id; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->passport_is; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->blood_group; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->blood_bag; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $donor->donation_date; ?></font></b></center></td>
               <td><a href="delete.php?id=<?php echo $people->id; ?>">Delete</a></td>
      </tr>
     <?php}
}?>
          </tr>

        </table>

1 个答案:

答案 0 :(得分:0)

尝试一下

<?php 
$db = new PDO('mysql:dbname=mypro_bms;host=localhost', 'root', '');
if (isset($_GET['q'])) {
  $q = $_GET['q'];
  $statement = $db->prepare("select * from donate where passport_IC like :passport_IC");
  $statement->execute([
    ':passport_IC' => '%' . $q .'%'
  ]);
} else {
  $statement = $db->prepare('select * from donate');
  $statement->execute();
}
$peoples = $statement->fetchAll(PDO::FETCH_OBJ);
?>
<?php
$statement = $db->prepare('select donation_date from tbl_user');
$statement->execute();
$donors = $statement->fetchAll(PDO::FETCH_OBJ);
?>
<table class="table table-bordered">
          <tr>
            <th>id</th>
            <th>Passport/IC</th>
            <th>Blood Group</th>
            <th>Blood Bag Type</th>
            <th>Donation Date</th>
            <th>Action</th>
        </tr>
<?php   foreach($peoples as $people){
            foreach($donors as $donor){ ?>
            <tr>
               <td><center><b><font color="black"><?php echo $people->id; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->passport_IC; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->blood_group; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $people->blood_bag; ?></font></b></center></td>
               <td><center><b><font color="black"><?php echo $donor->donation_date; ?></font></b></center></td>
               <td><a href="delete.php?id=<?php echo $people->id; ?>">Delete</a></td>
            </tr>
<?php }  } ?>
</table>