使用foreach和在PHP中一起生成表

时间:2015-01-06 18:11:24

标签: php mysql foreach while-loop

我已经做了类似下面这个表的几次,我使用 while 语句来填充表格的<td>部分,但我从未做过它之前是<td>的一部分填充了一个foreach语句,这让我感到困惑。

在这个例子中,我创建了一个表,然后根据数据库中的表数,用供应商列表填充第一列。 (每个供应商都有自己的表格。)

$supplierList = array();
$showTable = "SHOW TABLES from dbOne";
$getSuppliers = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getSuppliers)) {
    $supplierList[] = $row; 
    }
$supplierList = array_reduce($supplierList, 'array_merge', array());

现在我有一个包含所有供应商列表的数组,我用它在我的表中生成<td>

<table>
  <thead>
  <th style="text-align: center;">Supplier</th>
  <th style="text-align: center;">Earliest line</th>
  <th style="text-align: center;">Latest line</th>
  <th style="text-align: center;"># of total lines</th>


  </thead>
  <?php
  foreach ($supplierList as $subList) {
    $supplierName        = $subList;          

    $earlyExp            = "SELECT date FROM $subList ORDER BY date DESC LIMIT 1" ;
    $earlyExpQuery       = mysqli_query($con, $earlyExp);     
    $lateExp             = "SELECT date FROM $subList ORDER BY date ASC LIMIT 1" ;
    $lateExpQuery        = mysqli_query($con, $lateExp);
    $countLines          = "SELECT * from $subList";
    $countLinesQuery     = mysqli_query($con, $countLines);    
    $countLinesCount     = mysqli_num_rows($countLinesQuery);

    while ($row = mysqli_fetch_array($earlyExpQuery)) {
    $earlyExpDate    = $row['date'];

  ?>  
  <tbody>
  <td><img src = "/img/suppliers/<?= $supplierName ?>.png"></td>  
  <td style="text-align: center;"><?= $earlyExpDate ?></td>
  <td style="text-align: center;"><?= $lateExpDate ?></td>
  <td style="text-align: center;"><?= $countLinesCount ?></td>

  </tbody>
  <?php  
    }
    } 
  ?>
  </table>

表本身构建正确,并将每个供应商显示在唯一的行中。我无法弄清楚如何使用foreach语句中唯一供应商的信息来填充行的其他部分。

1 个答案:

答案 0 :(得分:0)

好的,我把它排除了。如果它可以帮助其他人,请按照以下方式进行操作:

此代码与我上面的帖子位于同一位置 - 因此位于表格的标题和正文之间。我不需要使用任何<tr>

<?php
  foreach ($supplierList as $subList) {
    $supplierName        = $subList;          

    $earlyExp            = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne ASC LIMIT 1" ;
    $earlyExpQuery       = mysqli_query($con, $earlyExp);             
    $lateExp             = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne DESC LIMIT 1" ;
    $lateExpQuery        = mysqli_query($con, $lateExp);
    $countLines          = "SELECT * from $subList";
    $countLinesQuery     = mysqli_query($con, $countLines);    
    $countLinesCount     = mysqli_num_rows($countLinesQuery);

    while ($row = mysqli_fetch_array($earlyExpQuery)) {
    $earlyExpDate    = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
    }    
    while ($row = mysqli_fetch_array($lateExpQuery)) {
    $lateExpDate    = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];

  ?>  

他们的关键是我必须关闭每个语句除了最后一个 - 在我关闭</tbody>标签之后但在</table>之前关闭这样:

</tbody>
<?php  
    }
    } 
  ?>
  </table>