如何在foreach中创建表

时间:2016-03-31 07:43:04

标签: php html

这是我的php代码

    <?php
include_once 'conectDB.php';
$sql ='SELECT CHECKLISTS.USER_ID, CHECKLISTS.ADMIN_ID, PROGRAMS.PROGRAM_NAME
FROM CHECKLISTS
LEFT JOIN COMPUTERS
ON CHECKLISTS.USER_ID = COMPUTERS.COMPUTER_NAME
LEFT JOIN ADMINS
ON CHECKLISTS.ADMIN_ID = ADMINS.USER_ID
LEFT JOIN CHECKLIST_PROGRAMS
ON CHECKLISTS.ID = CHECKLIST_PROGRAMS.CHECKLIST_ID
LEFT JOIN PROGRAMS
ON PROGRAMS.ID = CHECKLIST_PROGRAMS.PROGRAM_ID
';

$result = $connection->query($sql);
$checklists = array();
if ($result->num_rows > 0){

  while($row = $result->fetch_assoc()) {

    $checklists [] = $row;
  }

} else {
  echo '0 results';
}
$connection->close();
?>

这是HTML CODE 在这个foreach我不知道如何查询和创建表:(

<html>
<head>
  <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  <title>C.I.G. Check List</title></head>
  <body>
    <div class="container">
      <div class="row" style="margin-top:10px;">
        <div class="col-md-offset-2 col-md-8">
          <div class="row" style="margin-top:20px;">
          <div class="col-md-4"><h3>C.I.Gruop Checklist</h3></div>
          <div class="col-md-6" style="margin-top:19px; font-size:20px;"><a href="Addchecklist.php">New Item</a></div>
        </div></div>
      </div>
      <div class="col-md-2"></div>
      <div class="row" style="margin-top:20px;">
        <div class="col-md-offset-1 col-md-9">
          <!-- ส่วนแสดงผล -->
          <?php
          foreach ($checklists as $checklist) {
            print_r($checklist);
          }
          ?>
        </div>

      </div>

</div
  </body>
  </html>

3 个答案:

答案 0 :(得分:1)

你需要这样做: -

<table>
<?php
    foreach ($checklists as $checklist) {
        echo '<tr><td>'.$checklist.'</td></tr>';            
    }
?>
</table>

答案 1 :(得分:0)

在foreach中创建表格很简单。试试这个:

<table>
    <thead>
        <tr>
            <th>Check List</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($checklists as $checklist) {
            ?>
            <tr>
                <?php


                echo "<td>" . $checklist . "</td>";
                ?>

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

答案 2 :(得分:0)

一个选项可以是:

<table>
  <? foreach ($checklists as $checklist) { ?>
  <tr>
    <td>
      <? echo $checklist; ?>
    </td>
  </tr>
  <? } ?>
</table>

在循环中,您编写的所有内容都会重复次数与核对表项目一样多次