每第三个循环后插入 tr

2021-12-28 00:00:00 html-table php modulus

我正在用 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.

推荐答案

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

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

您可能需要为 first 和 last 做一些额外的 if's for first 和 last 之类的事情,但没有理由不使用模数 with a while.

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++
}

相关文章