[英]How to give alternating table rows different background colors using PHP
I have a table of data that is generated dynamically based on the contents stored in a mysql database.我有一个根据存储在 mysql 数据库中的内容动态生成的数据表。
This is how my code looks:这是我的代码的样子:
<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>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:现在,如果表格是静态的,那么我只需按重复顺序为每个交替表格行指定 2 种样式之一:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that.那将是结束。 However since the table rows are dynamically generated, how can I achieve this?但是,由于表行是动态生成的,我该如何实现呢?
或者你可以只使用 CSS:
table tr:nth-child(odd) { background-color: #ccc; }
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.它基本上检查 $x 是否可以被 2 整除。如果是,它是偶数。
PS if you haven't seen that style of if else query, it is called a ternary operator. PS 如果您还没有看到 if else 查询的那种风格,它被称为三元运算符。
<?
$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>
<?
}
?>
This is my working code !这是我的工作代码!
Here is my working part !这是我的工作部分! ` `
$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>";
` `
Set a variable to true/false or a number and then back again during each iteration.将变量设置为 true/false 或一个数字,然后在每次迭代期间再次返回。 Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
或者在 $i 是一个数字的 while 循环中使用模数运算符,例如 $i%2==0 并在三元语句或设置<tr>
的类值的东西中使用此条件
Easiest way to alternate row colors in PHP/HTML? 在 PHP/HTML 中交替行颜色的最简单方法?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
This is how I did it.我就是这样做的。 I declared a css class called "even" with all the styling i wanted.我用我想要的所有样式声明了一个名为“even”的css类。 Then looped through the scenario.然后循环遍历场景。 Hope it helps!希望能帮助到你!
<?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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.