首页 > 解决方案 > 使用 PHP foreach 制作一个 html 表格

问题描述

我尝试制作一个表格并用来自 MySql DB 和 foreach 的数据填充单元格:

<div class="single mb-5 mt-5">

<thead>
<tr>
  <th data-sort="string">Titre <i class="fa fa-sort"></i></th>
  <th data-sort="string">Genre <i class="fa fa-sort"></i></th>
  <th data-sort="string">Date de sortie <i class="fa fa-sort"></i></th>
  <th data-sort="string">Casting <i class="fa fa-sort"></i></th>
  </tr>
</thead>

<?php foreach($movie->getMovies() as $m) { ?>
<tbody>
  <tr>
  <td><a href="?p=single&id=<?php echo $m['id']; ?>" class="card-link"><?php echo $m['title']; ?></a></td>
  <td><?php echo $m['genres']; ?></td>
  <td><?php echo $m['release_date']; ?></td>
  <td><?php echo $m['release_date']; ?></td>
  </tr>
</tbody>
</table>
<?php } ?>

<script>
  $(document).ready(function($) { 
  $("#movie_list").stupidtable();
  }); 
</script>

结果是:

表格.jpg

有足够的数据有 21 个表行,但只有一个。

你能告诉我我错过了什么吗?

标签: phpforeachhtml-table

解决方案


while 语句对我有用:

<?php

$check = "SELEC * FROM table_name";
$execute = mysqli_query($connection, $check);
$i = 0;

while($row = mysqli_fetch_array($elements)) {
   $title = $row['title'];
   $genres = $row['genres'];
   $releaseDate = $row['release_date'];
   $releaseDate = $row['release_date'];

   $i++;
?>
<tbody>
   <tr>
   <td><a href="?p=single&id=<?php echo $m['id']; ?>" class="card-link"><?php echo $m['title']; ?></a></td>
      <td><?php echo $m['genres']; ?></td>
      <td><?php echo $m['release_date']; ?></td>
      <td><?php echo $m['release_date']; ?></td>
   </tr>
</tbody>
<?php } ?>

推荐阅读