Php foreach循环未生成正确的html表
我正在尝试从mysql获取的返回数据中获取此输出:Php foreach循环未生成正确的html表,php,html,loops,foreach,Php,Html,Loops,Foreach,我正在尝试从mysql获取的返回数据中获取此输出: <table> <thead> <tr> <th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th> </tr> </thead> &
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
但我得到的结果是一团糟。$featured\u tests
中的每个值都会对每个产品进行一次又一次的迭代,从而生成一个非常长的表。有人能帮我更正代码以获得理想的输出吗
结果如下:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
特征
液晶染料
自动驾驶
快速生成是的
双核
LCDNo
自动驾驶
快速生成否
双核
您正在最深的foreach中创建新行
以本例为例,制作您想要的:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>
第1列第2列
对于在foreach()
中迭代的每个项目,您都会打开一个
,但在循环之外,您只会关闭一次
。我看不出您的示例输出如何与代码相对应。发布的结果是最终结果,或者您已经发布了结果的一部分???正如您所提到的,它生成了一个很长的表。我想看看$rows
看起来像什么。ender,$row->special_features
是一个带有逗号分隔符的字符串,类似于Auto-Pilot、Red、Dual-core、Grey
,然后我使用inarray检查$featured\u tests
中的每个数组元素是否存在于$row->special\u features
中。
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>