php - 使用 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>
结果是:
有足够的数据有 21 个表行,但只有一个。
你能告诉我我错过了什么吗?
解决方案
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 } ?>
推荐阅读
- scala - Scala 3 隐式转换:比较值和文字
- node.js - 尝试发布扩展时,收到错误消息:Part URI is not valid per rules defined in the Open Packaging Conventions specification
- python - aws - 是否可以在 Typescript 中为 Python CDK 进行单元测试?
- momentjs - Moment JS diff() 在 10 月 31 日到 11 月 1 日之间返回 0 天
- python - 如何测试同一列中两个变量之间潜在的线性和/或分类或二元关系?
- angular - 刷新时找不到 Angular 页面
- gdb - GDB 步骤在 clang 上花费了很多时间
- swift - SwiftUI 中的这个视图叫什么?
- javascript - 每页最多存储 6 张卡片
- ios - 使用 VoiceOver 设置 contentOffset 后 UIScrollView didScroll 两次