我使用以下php循环显示mysql数据库中的所有行:
<tr>
<?php
$get_status = mysqli_query($link, "SELECT * FROM status ORDER BY status_order ASC");
while($status_result = mysqli_fetch_array($get_status) ) {
$status_name = htmlspecialchars($status_result['status_name']);
$status_order = htmlspecialchars($status_result['status_order']);
echo "<td><input type='checkbox' name='$status_name' value='$status_name'>$status_name</td>";
}
?>
</tr>
结果,看起来像是这样:
但我想要的是:
我想只显示5列,然后是5列,然后保留一列,依此类推...在php while循环中。我怎么能这样做?
答案 0 :(得分:2)
添加变量以跟踪数字,然后有条件地创建一个新行,如下所示:
<?php
$get_status = mysqli_query($link, "SELECT * FROM status ORDER BY status_order ASC");
$n = 0; //Keep track
while($status_result = mysqli_fetch_array($get_status) ) {
$status_name = htmlspecialchars($status_result['status_name']);
$status_order = htmlspecialchars($status_result['status_order']);
echo "<td><input type='checkbox' name='$status_name' value='$status_name'>$status_name</td>";
$n++; //Increase $n
if($n%5===0) echo "</tr><tr>"; //Make a new row every 5 iterations of loop
}