我想做一个表格,它在5列之后改变行。继续到50。现在为该行输入$i <= 4。示例:
1 | 2 | 5 | 4 | 5 |
6 | 7 | 8 | 9 | 10|<table width="300" border="1">
<tbody>
<tr>
<?php
$i = 0;
$x = 0;
do {
echo "<tr>";
$i++;
echo "<td> " . $i . "</td";
}
while ( $i <= 4 );
/* New Row */
echo "</td></tr>";
/* CONTINUE HERE */
?>
</tr>
</tbody>
</table>发布于 2020-11-03 09:08:38
使用到50的循环,并使用if ($i % 5 == 0)检查索引是否为5的倍数,以开始一个新行。
for ($i = 0; $i < $num_items; $i++) {
if ($i % 5 == 0) { // start a row every 5 items
echo "<tr>";
}
echo "<td>" . ($i + 1) . "</td>";
if ($i % 5 == 4) { // end a row 4 items later
echo "</tr>";
}
}
if ($i % 5 != 4) { // end the last row if there weren't a multiple of 5 items
echo "</tr>";
}发布于 2020-11-03 09:10:19
最简单的解决方案是在$i显示5次时插入tr。
<table width="300" border="1">
<tbody>
<?php
$i=1;
while ($i <=50)
{
if (($i-1) %5==0)
{echo "<tr>";}
echo "<td>". $i . "</td>";
$i++;
}
?>
</tbody>
</table>发布于 2020-11-03 10:48:22
这是我的解决方案。变量i%5 == 0将开始一个新行。
<table width="300" border="1">
<tbody>
<tr>
<?php
$i = 0;
$x = 50;
do {
$i++;
echo "<td> " . $i . "</td>";
if($i % 5 == 0)
{
echo "</tr>";
}
}
while ( $i < $x );
?>
</tr>
</tbody>
</table>https://stackoverflow.com/questions/64655305
复制相似问题