如何根据同一行中的相同值设置表tr背景颜色?
<?php
$result = mysql_query("SELECT * FROM `status` where username ='".$UserName."' ",$conn);
while($row=mysql_fetch_array($result))
{
echo '
<tr style="'.$bgcolor.'">
<td>
'.$row["Uv"].'
</td>
<td>
'.$row["Su"].'
</td>
<td> '.$row["Status"].' </td>
</tr> ';
}
?>
How the rows should look like 我想根据相同的uv改变颜色,如hmsid20161812(蓝色),hmsid20161815(棕色),hmsid20161810(黄色)意味着我们在数组中给出颜色代码或名称,但是根据紫外线打印。
答案 0 :(得分:0)
您需要在颜色之前添加css的background-color:
属性。
更改
<tr style="'.$bgcolor.'">
与
<tr style="background-color: '.$bgcolor.'">
答案 1 :(得分:0)
您可以尝试这样的操作,并且还应将所有mysql_
函数更改为至少mysqli_
个函数。最好试试PDO。看看这个令人生畏的博客here
<?php
$bgcolor = "blue"; //you have to define it somewhere like here
$bgcolor2 = "red"; //you have to define it somewhere like here
$bgcolor3 = "yellow"; //you have to define it somewhere like here
$result = mysqli_query("SELECT * FROM `status` where username ='".$UserName."' ",$conn);
while($row = mysqli_fetch_array($result))
{
if ($row['Uv'] == "hmsid20161812") {
echo "<tr style='background-color: $bgcolor'>
<td>
".$row['Uv']."
</td>
<td>
".$row["Su"]."
</td>
<td> ".$row["Status"]." </td>
</tr> ";
} elseif ($row['Uv'] == "hmsid20161815") {
echo "<tr style='background-color: $bgcolor2'>
<td>
".$row['Uv']."
</td>
<td>
".$row["Su"]."
</td>
<td> ".$row["Status"]." </td>
</tr> ";
}
elseif ($row['Uv'] == "hmsid20161810") {
echo "<tr style='background-color: $bgcolor3'>
<td>
".$row['Uv']."
</td>
<td>
".$row["Su"]."
</td>
<td> ".$row["Status"]." </td>
</tr> ";
}
}
?>