如何使用 PHP 为交替的表格行提供不同的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3034511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to give alternating table rows different background colors using PHP
提问by Sam
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?
那将是结束。但是,由于表行是动态生成的,我该如何实现呢?
回答by Jason Palmer
Or you could just use CSS:
或者你可以只使用 CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
table tr:nth-child(odd) {
background-color: #ccc;
}
回答by Aaron Harun
<?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 整除。如果是,它是偶数。
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
PS 如果您还没有看到 if else 查询的那种风格,它被称为三元运算符。
回答by meder omuraliev
Set a variable to true/false or a number and then back again during each iteration. 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>
将变量设置为 true/false 或一个数字,然后在每次迭代期间再次返回。或者在 $i 是一个数字的 while 循环中使用模数运算符,例如 $i%2==0 并在三元语句或设置类值的东西中使用此条件<tr>
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++;
}
回答by viorel
<?
$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 !
这是我的工作代码!
回答by YakuZa
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>";
`
`
回答by Ismailp
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
我就是这样做的。我用我想要的所有样式声明了一个名为“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);
?>

