用PHP表省区和分区

用PHP表省区和分区

我的数据库中有表Province District和Subdistrict,我想将其打印为html表中的输出,如下所示:

php - 用PHP表省区和分区-LMLPHP

但我得到这个:

php - 用PHP表省区和分区-LMLPHP

这是我的代码:

<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
  <td> <?php echo $orderProv; ?></td>
  <td> <?php echo $QueryProvince->nameProv; ?></td>
  <?php
  //$QueryDistricts = Query District;
  foreach ($QueryDistricts as $QueryDistrict ) {
  ?>
  <td> <?php echo $QueryDistrict ->nameDist; ?></td>
  <?php
  }
  ?>
</tr>
<?php
}
?>


请帮我,

非常感谢。

最佳答案

您需要关闭每个循环foreach ($QueryDistricts as $QueryDistrict ),然后打开新行。试试这样的事情-

<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
  <td> <?php echo $orderProv; ?></td>
  <td> <?php echo $QueryProvince->nameProv; ?></td>
  <?php
  //$QueryDistricts = Query District;

  $i = 0; // simple counter

  foreach ($QueryDistricts as $QueryDistrict ) {

      if($i>0){ // if not the 1st row, echo new row start
  ?>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <?php
      }
  ?>
  <td> <?php echo $QueryDistrict ->nameDist; ?></td>
</tr>
  <?php
   $i++;
  }
}
?>

关于php - 用PHP表省区和分区,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33907746/

10-09 19:18