如何在php中根据相同的值设置表tr背景颜色

时间:2016-12-09 06:37:26

标签: php css mysql

如何根据同一行中的相同值设置表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(黄色)意味着我们在数组中给出颜色代码或名称,但是根据紫外线打印。

2 个答案:

答案 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> ";
    }
}
?>