Areg Karapetyan Areg Karapetyan - 6 months ago 32
HTML Question

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

I need to have rows with value less than 50000 background red color, more than 100000 background blue color and others have no color. Problem is that it is taking a general color which is written in "else" part. Here is my code:

<?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>

Answer

Like this maybe?

Side note; since I don't know PHP, I assume its syntax is correct

<?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>