css - 在表格的交替行上显示不同的样式(在循环内)

标签 css mysql loops html-table

我正在用查询结果填充一个表,我希望行的样式交替,即 <tr><tr class="alt> . (不使用 CSS3)

我使用 while 循环在我的表中为结果集中的每个结果显示一行。

我该怎么做?我迷路了。

请帮忙。

我的解决方案:

   if($i % 2) { //this means if there is a remainder
     echo "<tr class='alt'>";
   } else { //if there isn't a remainder we will do the else
     echo "<tr>";
   }

$i++;

最佳答案

有规律的for i循环,你会使用 modulus (这是 % 运算符)在 i 上看看它是否是 2 的倍数。

在 while 循环中,您需要使用另一种增量器,也许只是在循环之前声明一个增量器并在每次通过时递增它:

$i = 0;
while (condition) {
    $class = (i%2 == 0) ? 'alt' : '';
    echo '<tr class="' + $class + '">';
    $i++;
}

警告:我写的 PHP 不多,请将以上内容视为伪代码。它应该非常接近工作,如果不是直接正确的话。

关于css - 在表格的交替行上显示不同的样式(在循环内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503025/

相关文章:

html - 在 Firefox 中替代 -webkit-outer-spin-button 和 -webkit-inner-spin-button

html - Header 中的 CSS 级联顺序

html - @font-face 在安装 ParcelJS 后不工作

mysql - 错误 1215 : cannot add foreign key contraint

php - 使用 PHP 将 mysql 表导出到 CSV - header 问题

php - 用户 'domain\name'登录失败

c - 如何修复循环?

html - 页面内容 "erasing"背景

bash - 使用 GNU Parallel 并行化嵌套 for 循环

javascript - 为什么这个 for-of 循​​环中的对象解构不起作用?