gpt4 book ai didi

php - html表中的多个foreach循环

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:24 26 4
gpt4 key购买 nike

在我的数据库中,我有 2 个表。

轴表:

 ______________________________________
| axle_id | train_id | axle | distance |
|_________|__________|______|__________|
| 1 | 1 | 1 | 20 |
| 2 | 1 | 2 | 50 |
| 3 | 1 | 3 | 200 |
| 4 | 1 | 4 | 50 |
| 5 | 1 | 5 | 200 |
| 6 | 1 | 6 | 50 |
| 7 | 1 | 7 | 200 |
| 8 | 1 | 8 | 50 |
|_________|__________|______|__________|

转向架台:

 ___________________________________________
| bogie_id | axle_id | train_id | bogie_nr |
|__________|_________|__________|__________|
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | 1 |
| 3 | 3 | 1 | 2 |
| 4 | 4 | 1 | 2 |
| 5 | 5 | 1 | 3 |
| 6 | 6 | 1 | 3 |
| 7 | 7 | 1 | 4 |
| 8 | 8 | 1 | 4 |
|__________|_________|__________|__________|

现在我想在我的页面上以这样的表格显示这些结果:

 _____________________
| bogie_nr | axle |
|__________|_________|
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 3 | 6 |
| 4 | 7 |
| 4 | 8 |
|__________|_________|

我试着用这段代码来做到这一点:

<table id="end_result_axle_bogies">
<tr>
<th>Train Axles</th>
<th>Train Bogies</th>
</tr>

<!--Show the end result!-->
<?php
$show_end_table = $database->axles($_GET['train_id']);
$show_end_table2 = $database->bogies($_GET['train_id']);
foreach($show_end_table as $end_axle_table){
echo "<tr>" . "<td>" . $end_axle_table['axle'] . "</td>";
}
foreach($show_end_table2 as $end_axle_table2){
echo "<td>" . $end_axle_table2['bogie_nr'] . "</td>" . "</tr>";
}
?>
</table>

输出有效(向我显示正确的信息)。但该表不能很好地工作。它向我展示了这个结果:

End results Shown

(将文本设为绿色以查看 div 是否正常工作。至于有效)。

有人知道如何使表格正常工作吗?

最佳答案

试试这个:

<?php
$show_end_table = $database->axles($_GET['train_id']);
$show_end_table2 = $database->bogies($_GET['train_id']);
$table2 = "";
foreach($show_end_table2 as $end_axle_table2){
$table2[] = $end_axle_table2['bogie_nr'];
}
foreach($show_end_table as $key => $end_axle_table){
echo "<tr>";
echo "<td>" . $end_axle_table['axle'] . "</td>";
echo "<td>" . $table2[$key] . "</td>";
echo "</tr>";
}
?>

改变foreach。就像你现在做的那样,它是 <tr><td>1</td><tr><td>2</td> ... Echo all in one foreach 是更好的方法。

关于php - html表中的多个foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com