How to change cell color depending on values in php?

by tressie.damore , in category: PHP General , 6 months ago

How to change cell color depending on values in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 6 months ago

@tressie.damore 

To change the cell color depending on values in PHP, you can use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
$value = 10; // Example value

// Define color codes based on the value
if ($value < 5) {
    $color = "red";
} elseif ($value >= 5 && $value < 10) {
    $color = "orange";
} elseif ($value >= 10 && $value < 15) {
    $color = "yellow";
} else {
    $color = "green";
}

// Echo the HTML table with the colored cell
echo "<table>";
echo "<tr><td style='background-color: $color;'>$value</td></tr>";
echo "</table>";
?>


In the code above, the variable $value is the value that determines the color of the cell. The color is defined based on different ranges of values. In this example, if the value is less than 5, the cell will be colored red, if it is between 5 and 10, the cell will be orange, if it is between 10 and 15, the cell will be yellow, and otherwise, it will be green.


The HTML table is then echoed with the colored cell using the inline CSS background-color property to set the cell color dynamically based on the value.