I am trying to change the TD background color dinamically but it makes me crazy because I really don’t know how to fix the code.
This is my code:
<td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH; ?>">
<?php
if (($row['TotalMatch']) > 9){
$percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100;
echo sprintf("%.2f", $percover05sh);
if ($percover05sh > 80){
$backgroundColorOver05SH = "green";
} elseif ($percover05sh >= 70 and $percover05sh <= 79.99 ){
$backgroundColorOver05SH = "yellow";
} else {
$backgroundColorOver05SH = "red";
}
I think I’m in the right direction, but I can’t get the right solution.
Any suggestion please? Thanks!
EDIT: This code now it’s working! This is my full code:
<?php
if (($row['TotalMatch']) > 9){
$percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100;
if ($percover05sh >= 80){
$backgroundColorOver05SH = "green";
} elseif ($percover05sh >= 70 && $percover05sh < 80 ){
$backgroundColorOver05SH = "yellow";
} else {
$backgroundColorOver05SH = "red";
}
}else{
echo 'No Bet';
} ?>
<td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH;?>;">
<?php
echo sprintf("%.2f", $percover05sh);
?>
</td>
2
Answers
Apart from changing the order in your code (as written in the comments to the question), I would add a semicolon after the background-color value echoed by PHP in the style attribute, i.e.
So first:
If the color doesn’t change maybe it is set by a different CSS rule or script?