Php 如何以交替行颜色显示表格中的数据
我正在尝试使用行的可选颜色方案在表中显示数据 我正在使用下面的代码显示结果Php 如何以交替行颜色显示表格中的数据,php,mysql,sql,Php,Mysql,Sql,我正在尝试使用行的可选颜色方案在表中显示数据 我正在使用下面的代码显示结果 <?php while($row = mysql_fetch_array($sql)) { echo "<tr> "; echo "<td>" .$row[username] . "</td>"; echo "<td>" .$row[total] . "</td>";
<?php
while($row = mysql_fetch_array($sql)) {
echo "<tr> ";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
?>
我想使用此格式显示数据:
<tbody>
<tr class="alt"><td>Data</td><td>points</td></tr>
<tr><td>Data</td><td>points</td></tr>
<tr class="alt"><td>Data</td><td>points</td></tr>
<tr><td>Data</td><td>points</td></tr>
<tr class="alt"><td>Data</td><td>point</td></tr>
</tbody>
数据点
数据点
数据点
数据点
数据点
请指导使用模运算符:
<?php
$rowNum=0;
while($row = mysql_fetch_array($sql)) {
echo "<tr ";
if ($rowNum++ % 2 == 0) {
echo 'class="alt"';
}
echo " >";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
使用模运算符:
<?php
$rowNum=0;
while($row = mysql_fetch_array($sql)) {
echo "<tr ";
if ($rowNum++ % 2 == 0) {
echo 'class="alt"';
}
echo " >";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
尝试如下所示
<?php
$i = 0;
while($row = mysql_fetch_array($sql)) {
$class = "";
if($i % 2 == 0)
$class = "class='alt'";
echo "<tr $class> ";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
$i++;
}
?>
尝试如下所示
<?php
$i = 0;
while($row = mysql_fetch_array($sql)) {
$class = "";
if($i % 2 == 0)
$class = "class='alt'";
echo "<tr $class> ";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
$i++;
}
?>
使用计数器:
$i = 1
while($row = mysql_fetch_array($sql))
{
echo "<tr" . (($i++%2) ? '' : ' class="alt"') . ">";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
$i=1
while($row=mysql\u fetch\u数组($sql))
{
回声“;
回显“$行[用户名]”;
回显“$行[总计]”;
回声“;
}
使用计数器:
$i = 1
while($row = mysql_fetch_array($sql))
{
echo "<tr" . (($i++%2) ? '' : ' class="alt"') . ">";
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
$i=1
while($row=mysql\u fetch\u数组($sql))
{
回声“;
回显“$行[用户名]”;
回显“$行[总计]”;
回声“;
}
使用css:
tr:nth-child(odd) {
background-color: #ccc;
}
此小提琴显示了如何选择偶数
、奇数
、每个n'th
、或每个n'th+x
行来设置样式,只需CSS即可:
使用css:
tr:nth-child(odd) {
background-color: #ccc;
}
此小提琴显示了如何选择偶数
、奇数
、每个n'th
、或每个n'th+x
行来设置样式,只需CSS即可:
您需要使用来获得将$rowNum
除以2的余数。基本上,对于每一行,将一个添加到$rowNum
,然后检查它是偶数还是奇数。另外,请确保在用户名
和总计
中添加引号。因为没有将它们括在字符串中,所以它们被视为常量
<?php
$rowNum = 0;
while($row = mysql_fetch_array($sql)) {
echo ($rowNum++ % 2 == 0) ? "<tr class=\"alt\">" : "<tr>";
echo "<td>" .$row['username'] . "</td>";
echo "<td>" .$row['total'] . "</td>";
echo "</tr>";
}
?>
您需要使用将$rowNum
除以2的余数。基本上,对于每一行,将一个添加到$rowNum
,然后检查它是偶数还是奇数。另外,请确保在用户名
和总计
中添加引号。因为没有将它们括在字符串中,所以它们被视为常量
<?php
$rowNum = 0;
while($row = mysql_fetch_array($sql)) {
echo ($rowNum++ % 2 == 0) ? "<tr class=\"alt\">" : "<tr>";
echo "<td>" .$row['username'] . "</td>";
echo "<td>" .$row['total'] . "</td>";
echo "</tr>";
}
?>
您可以使用count变量对循环进行计数,然后使用count%2每隔一次迭代获取一次
<?php
$count = 0; //The count variable
while($row = mysql_fetch_array($sql)) {
echo "<tr" // open the tag
if($count%2 == 0) {
echo "class='alt'"; // Ever other iteration, this will run
}
echo ">"; // close the tag
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
?>
您可以使用count变量对循环进行计数,然后使用count%2每隔一次迭代获取一次
<?php
$count = 0; //The count variable
while($row = mysql_fetch_array($sql)) {
echo "<tr" // open the tag
if($count%2 == 0) {
echo "class='alt'"; // Ever other iteration, this will run
}
echo ">"; // close the tag
echo "<td>" .$row[username] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
}
?>
语法错误,意外的'if'(T_if),应为','或';'语法错误,意外的“if”(T_if),应为“,”或“;”同样的结果也是原创的query@aleksay,我想对多个表使用此查询。行的范围从5到30+。相同的结果是原始的query@aleksay,我想对多个表使用此查询。行的范围为5到30+。结果与原始查询相同mine@user3500806我的稍有不同。我更改了while循环的第一行,结果与的原始查询相同mine@user3500806我的稍有不同。我更改了while循环的第一行