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

时间:2016-05-15 20:31:34

标签: php html css

我需要的行的背景红色小于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>

1 个答案:

答案 0 :(得分:0)

喜欢这样吗?

旁注;因为我不懂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>