如何使用PHP为交替的表格行指定不同的背景颜色
我有一个基于mysql数据库中存储的内容动态生成的数据表 这就是我的代码的外观:如何使用PHP为交替的表格行指定不同的背景颜色,php,Php,我有一个基于mysql数据库中存储的内容动态生成的数据表 这就是我的代码的外观: <table border="1"> <tr> <th>Name</th> <th>Description</th> <th>URL</th> </tr> <?php $query = mysql_query("SE
<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>
那就结束了。但是,由于表行是动态生成的,如何实现这一点?将变量设置为true/false或数字,然后在每次迭代期间返回。或者在while循环中使用模运算符,例如$i%2==0,其中$i是一个数字,并在三元语句或设置
$i=0;
while($row=mysql\u fetch\u assoc($result)){
回显“.$row['something']”;
$i++;
}
或者您可以使用CSS:
表tr:n个子项(奇数){
背景色:#ccc;
}
我就是这样做的。我声明了一个名为“偶数”的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);
?>
这是我的工作代码 这是我的工作部分!
`
$i=1;
while($row=mysqli\u fetch\u数组($result)){
如果($i%2==0)
{
回声';
}
其他的
{
回声';
}
$i++;
回显“$row['blah']”;
回显“$row['blah_blah']”;
回声“;
}
回声“;
`查看模运算符=)这不是一个相反的while循环。;)不,不是,我只是说了。最好更改我的语句。除了为$color
使用字符串值,您还可以(更好)使用布尔值:$color=true
(您还需要修改示例的其余部分)如果直接进入html,则点不应出现在类名中;)当然,如果不使用css,您只需将类变量的位置替换为bgcolour ie$class=($x%2==0)''白色“:“灰色”代码>echo”“代码>bgcolor
对于传统网站是受支持的,但您不应该使用它。请注意,OP的代码中“background”是大写的:grayBackground
。您如何修改它以不包括顶行?我知道了,但是如果我希望奇数行为一种颜色,偶数行为一种颜色,该怎么办,但最上面一排也要上色吗?这可能吗?然后您将为tr:first child{background color:#foo;}表创建一个样式
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
<?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);
?>
<?
$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>
<?
}
?>
$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>";