每第三个循环后插入 tr

发布于 2024-12-28 17:47:08 字数 120 浏览 3 评论 0原文

我正在用 PHP 制作一个论坛。我必须在表格中显示所有论坛类别,为此,我使用了 while 循环。但是,我希望每个表行中只有 3 个 td。为了循环遍历类别,我在查询中使用了 while 循环,所以我认为我不能在这里使用模数。

I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半枫 2025-01-04 17:47:08

为什么不能使用模数?只需在某处添加一个计数器,如果它命中 % 3 == 0 则重置计数器并执行您的操作。

您可能需要为第一个和最后一个以及类似的东西做一些额外的 if ,但是没有理由不使用一段时间的模数。

$i=0;
while(guard()){
    if($i % 3 == 0){
       //ploing
    }
 $i++
}

Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.

You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.

$i=0;
while(guard()){
    if($i % 3 == 0){
       //ploing
    }
 $i++
}
烟织青萝梦 2025-01-04 17:47:08

此代码将关闭所有额外的行:

<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
        echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
        echo "</tr>";
    }
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
        echo "<td></td>";
    }
    echo "</tr>";
}
?>
</table>

This code will close any extra rows:

<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
        echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
        echo "</tr>";
    }
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
        echo "<td></td>";
    }
    echo "</tr>";
}
?>
</table>
送舟行 2025-01-04 17:47:08

我还没有测试代码,但逻辑应该有效:

<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
    if($i == 0){
        echo"<TR>";
    }
    echo"<td>".$row[0]."<TD>";
    $i++;
    if($i == 3)
    {
        $i = 0;
        echo"</tr>"
    }
}
if($i ==1){
    echo "<td></td><td></td></tr>";
}
if($i ==2)
{
    echo "<td></td></tr>";
}
?>
<table>

I haven't tested the code, but the logic should work:

<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
    if($i == 0){
        echo"<TR>";
    }
    echo"<td>".$row[0]."<TD>";
    $i++;
    if($i == 3)
    {
        $i = 0;
        echo"</tr>"
    }
}
if($i ==1){
    echo "<td></td><td></td></tr>";
}
if($i ==2)
{
    echo "<td></td></tr>";
}
?>
<table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文