I'm trying to dynamically change the TD background color, but it's driving me crazy because I don't really 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 on the right track, but I can't find the right solution. Any suggestions? Thanks!
Edit: This code now works! This is my complete 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>
In addition to changing the order in the code (as written in the question comments), I would also add a semicolon after the background color value echoed by PHP in the style attribute, which is
So first: