php - 确定 php while 循环中的每个第三项?

标签 php html css while-loop html-lists

我正在尝试从数据库中获取数据并将它们列在 <li> 中列表。我试图找出每个第三个列表项并给它一个不同的 li 类?这是我的代码

<?php
while ($row = mysql_fetch_array($getupdates)){
?>
<li id="update" class="group">
   blah blah blah
</li>
<?php } ?>

所以基本上每三个元素我想给它一个不同的 li 类

<li id="update" class="group third">

最佳答案

在你的 while 循环中有一个计数器。我们称它为 $i。在你的循环中,添加这个:

$i++;
if ($i % 3 == 0) {
   //do something you'd do for the third item
}
else { //default behavior }

关于php - 确定 php while 循环中的每个第三项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833866/

相关文章:

php - Codeigniter:在非对象上调用成员函数 result_array()

html - 如何阻止div继承父css样式

html - 在屏幕上拉伸(stretch) 1 个 div 中包含的图像

URL 查询的 PHP parse_str 返回键名中包含 'amp' 的数组键

php - 选择 Doctrine 和 MS SQL 2008 的问题

html - 如何设置溢出 :hidden for the HTML but set scroll for the tables?

javascript - pretty-print 在推特 Bootstrap 中不起作用

javascript - jQuery - 数组没有显示正确的值

PHP:发送WORD文档文件下载

html - 如何修复 css 关键帧动画的闪烁