【问题标题】:How to give alternating table rows different background colors using PHP如何使用 PHP 为交替的表格行提供不同的背景颜色
【发布时间】:2011-03-03 08:20:14
【问题描述】:

我有一个根据存储在 mysql 数据库中的内容动态生成的数据表。

这就是我的代码的样子:

<table border="1">
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>URL</th>
    </tr>
    <?php
        $query = mysql_query("SELECT * FROM categories");
        while ($row = mysql_fetch_assoc($query)) {
            $catName = $row['name'];
            $catDes = $row['description'];
            $catUrl = $row['url'];

            echo "<tr class=''>";
            echo "<td>$catName</td>";
            echo "<td>$catDes</td>";
            echo "<td>$catUrl</td>";
            echo "</tr>";
        }
    ?>
</table>

现在,如果表格是静态的,那么我只需按重复顺序为每个交替表格行分配 2 种样式之一:

.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }

到此为止。但是由于表格行是动态生成的,我该如何实现呢?

【问题讨论】:

  • 查看取模运算符 =)

标签: php


【解决方案1】:

或者你可以只使用 CSS:

table tr:nth-child(odd) { background-color: #ccc; }

【讨论】:

  • 你怎么能修改这个不包括第一行?
  • 我明白了,但是如果我想要奇数行一种颜色,偶数行一种颜色,而且顶行到顶部有颜色怎么办?这可能吗?
  • 然后你会为表格创建一个样式 tr:first-child { background-color: #foo; }
【解决方案2】:
<?php 

$x++; 

$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';

echo "<tr class='$class'>";

?>

它基本上检查 $x 是否能被 2 整除。如果是,它是偶数。

附:如果你没有见过这种风格的 if else 查询,它被称为三元运算符。

【讨论】:

  • 如果直接进入 html,点不应该出现在类名中;)
  • 不使用css当然你可以简单地将类变量的位置替换为bgcolour,即$class = ($x%2 == 0)? 'white': 'gray';echo "&lt;tr bgcolor='$class'&gt;";
  • bgcolor 支持旧网站,但您不应该使用它。
  • 请注意,“背景”在 OP 代码中大写:grayBackground
【解决方案3】:

将变量设置为真/假或数字,然后在每次迭代期间再次返回。或者在 $i 是一个数字的 while 循环中使用模运算符,例如 $i%2==0,并在三元语句中使用此条件或设置&lt;tr&gt; 的类值的东西

Easiest way to alternate row colors in PHP/HTML?

$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
 echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}

【讨论】:

  • 这不是 reverse while 循环。 ;)
  • 不,它不是——我刚刚说过。最好改变我的说法。
【解决方案4】:
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
  if($color==1){
    echo '<tr bgcolor="">';
    $color="2";
  } else { 
    echo '<tr bgcolor="#dcdcdc">';
    $color="1";
  }
  ?><td align="left" width="40"><a href=""></a><?= $line[name] ?></td>

<?
}
?>

这是我的工作代码!

【讨论】:

  • 除了使用$color 的字符串值,您还可以(更好)使用布尔值:$color = true(您还需要修改示例的其余部分)
【解决方案5】:

这是我的工作部分! `

 $i=1;
 while($row = mysqli_fetch_array($result)) {
 if($i%2==0)
 {
     echo '<tr bgcolor="#FFFF00">';
 }
 else
 {
     echo '<tr bgcolor="#99FFCC">';
 }
     $i++;
     echo "<td>" . $row['blah'] . "</td>";
     echo "<td>" . $row['blah_blah'] . "</td>";
     echo "</tr>";

 }
 echo "</table>";

`

【讨论】:

    【解决方案6】:

    我就是这样做的。我用我想要的所有样式声明了一个名为“even”的 CSS 类。然后循环播放场景。希望对您有所帮助!

    <?php
        include 'connect.php';
        echo "<table id='hor-zebra'>";
        $i = 0;
        while($row = mysql_fetch_array($result))
        {
           if($i%2 == 0)
           {
              echo "<tr class='even'>";
              echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
              echo "</tr>";
           }
    
           else
           {
              echo "<tr>";
              echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
              echo "</tr>";
           }
           $i++;
        }
        echo "</table>";
    
        mysql_close($con);
    
      ?>
    

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      相关资源
      最近更新 更多