我在我的应用程序中有一个foreach语句,它回显了我的数据库结果列表:
foreach($featured_projects as $fp) {
echo '
';
';
echo $fp['project_name'];
echo '
}
?>
我想要:
在每第三个结果上,给div一个不同的类.我怎样才能做到这一点?
解决方法:
您可以使用计数器和modulo/modulus运算符,如下所示:
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '
';
';
echo $fp['project_name'];
echo '
}
?>
标签:php,echo,foreach
来源: https://codeday.me/bug/20191006/1861590.html