我想用不同的类设置每个第一和第二tr
。对于我的代码,我只在每次尝试中得到"奇数"。有人知道出了什么问题吗?
$rowCount = 0;
if ($rowCount++ % 2 == 1 ) :
echo "<tr class='even'>";
else:
echo "<tr class='odd'>";
endif;
试试这个(保持$rowCount设置在循环之外):
for($row = 0; $row < $rowTotal; $row++)
{
echo "<tr class='".($row % 2 ? "even" : "odd")."'>";
}
您的逻辑实现方向错误
$rowCount = 0;//This was always initializing your count to 0
结果总是奇数类添加
改成:
for ($rowCount = 0;$rowCount<$total; $rowCount++) {
if ($rowCount % 2 == 1 ) :
echo "<tr class='even'>";
else:
echo "<tr class='odd'>";
endif;
}
或者您可以简单地使用ternary
运算符作为
for ($rowCount=0; $rowCount<$total; $rowCount++) {
echo "<tr class='".($rowCount % 2 == 0 )?'odd':'even'."'>";
}