How to change cell color depending on values in php?

by arnoldo.moen , in category: PHP General , 2 years ago

How to change cell color depending on values in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , a year ago

@arnoldo.moen 

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
$value = 20; // this is the value you want to check
$color = '';

if($value < 10){
    $color = 'red';
} elseif($value >= 10 && $value < 20){
    $color = 'orange';
} elseif($value >= 20 && $value < 30){
    $color = 'yellow';
} else {
    $color = 'green';
}
?>

<td style="background-color: <?php echo $color; ?>"><?php echo $value; ?></td>


In this example, the $value variable is set to 20. Depending on the value of $value, the $color variable is set to a specific color. The cell's background color is then set to the value of the $color variable using inline CSS. Finally, the $value is displayed within the cell.


You can modify the conditional statements and color values in the code snippet to suit your specific requirements.

Related Threads:

How to change cell color depending on values in php?
How to set cell color in phpexcel?
How to change alignment of each cell in html table?
How to change the label color in chart.js?
How to change a color in an area of a canvas?
How to change color-scheme on bitbucket?