我有一个数组,我把它们放在表格中,我需要改变表格的背景颜色


I have an array and I put them in table, I need to change background color of table

我需要有值小于50000背景红色、大于100000背景蓝色的行,其他行没有颜色。问题是,它采用了写在"其他"部分的通用颜色。这是我的代码:

<?php
$lll = array("blank" => 46822,
              "default" => 79254,
              "display" => 49511,
              "document" => 88839,
              "examples" => 81802,
              "for" => 52427,
              "function" => 65907,
              "height" => 47510,
              "left" => 45011,
              "literal" => 45884,
              "reference" => 61932,
              "tag" => 148895,
              "target" => 339410,
              "this" => 45275,
              "title" => 91441,
              "top" => 335154,
              "type" => 65768,
              "width" => 68883);    
foreach ($lll as $key => $value)
        {
            if($value < 50000)
            {
                $color = "#FF0000";
            }
            else if($value > 100000)
            {
                $color = "#0012FF";
            }
            else
            {
                $color = "#FFFFFF";
            }
        }
?>
<html>
<head>
    <title>Web page</title>
    <style>
        td{background-color: <?php echo $color;?>}
        tr{background-color: <?php echo $color;?>}
    </style>
</head>
<body>
    <?php
        echo "<table border = '1'>";
        echo "<td>Number</td><td>Value</td>";
            foreach ($lll as $key => $value)
            {
                echo "<tr>";
                echo "<td>$key</td>";
                echo "<td>$value</td>";
                echo "</tr>";
            }
        echo "</table>";
    ?>
</body>
</html>

也许是这样?

旁注;由于我不懂PHP,我认为它的语法是正确的

<?php
$lll = array("blank" => 46822,
              "default" => 79254,
              "display" => 49511,
              "document" => 88839,
              "examples" => 81802,
              "for" => 52427,
              "function" => 65907,
              "height" => 47510,
              "left" => 45011,
              "literal" => 45884,
              "reference" => 61932,
              "tag" => 148895,
              "target" => 339410,
              "this" => 45275,
              "title" => 91441,
              "top" => 335154,
              "type" => 65768,
              "width" => 68883);
?>
<html>
<head>
    <title>Web page</title>
    <style>
        .red   {background-color: #F00;}
        .blue  {background-color: #0012FF;}
        .white {background-color: #FFF;} 
    </style>
</head>
<body>
    <?php
        echo "<table border = '1'>";
        echo "<tr><td>Number</td><td>Value</td></tr>";
            foreach ($lll as $key => $value)
            {
              if($value < 50000)
              {
                echo "<tr class='red'>";
              }
              else if($value > 100000)
              {
                echo "<tr class='blue'>";
              }
              else
              {
                echo "<tr class='white'>";
              }
              echo "<td>$key</td>";
              echo "<td>$value</td>";
              echo "</tr>";
            }
        echo "</table>";
    ?>
</body>
</html>