[英]How to foreach loop in table 3 column using php
I have code like this 我有这样的代码
<table>
<tr>
<th>No</th>
<th>Title</th>
<th>Name</th>
</tr>
foreach($value as $item){
echo "<tr>";
echo "<td>".$item['no']."</td>";
echo "<td>".$item['title]."</td>";
echo "<td>".$item['data']."</td>";
echo "</tr>";
}
</table>
For example I have 5 items in my loop. 例如,我的循环中有5个项目。 My loop output now something like this:
我的循环输出现在是这样的:
| No | Title | Name |
|----|----------|-------|
| 1 | Book | Susan |
| 2 | Comic | Budi |
| 3 | Recipe | Anwar |
| 4 | Magazine| Leo |
| 5 | Novel | Clara |
But I want output like this with the looping header also 但是我也想要这样的输出与循环头
You may try array_chunk
to split array as 3 items in each row first. 您可以尝试使用
array_chunk
将数组array_chunk
分成每行3个项目。
$value = [
['no' => '1', 'title' => 'Book', 'data' => 'Susan'],
['no' => '2', 'title' => 'Comic', 'data' => 'Budi'],
['no' => '3', 'title' => 'Recipe', 'data' => 'Anwar'],
['no' => '4', 'title' => 'Magazine', 'data' => 'Leo'],
['no' => '5', 'title' => 'Novel', 'data' => 'Clara']
];
$value = array_chunk($value, 3);
echo '<table>';
foreach ($value as $row) {
echo '<tr>';
foreach ($row as $item) {
echo '<td>'.$item['no'].'</td>';
echo '<td>'.$item['title'].'</td>';
echo '<td>'.$item['data'].'</td>';
}
echo '</tr>';
}
echo '</table>';
You can modify and use modulus operator to close raw and open raw. 您可以修改并使用模数运算符关闭原始文件和打开原始文件。
replace your loop data and test it. 替换循环数据并进行测试。
$i = 1;
echo '<tr>';
foreach ($row as $item) {
echo '<td>'.$item['no'].'</td>';
echo '<td>'.$item['title'].'</td>';
echo '<td>'.$item['data'].'</td>';
if($i % 3 == 0) {
echo '</tr><tr>';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.