skip to Main Content

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


  1. 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.

    <td data-column="% Over 0.5 SH" style="background-color: <?php echo $backgroundColorOver05SH; ?>;">
    
    Login or Signup to reply.
  2. So first:

     
    <?php 
    if (($row['TotalMatch']) > 9){ 
    $percover05sh = $row['OK_05sh'] / $row['TotalMatch'] * 100; 
    echo sprintf("%.2f", $percover05sh);
    
    
    if ($percover05sh > 80){
        $backgroundColorOver05SH = "green";
    } elseif ($percover05sh >= 70 && $percover05sh < 80  ){
        $backgroundColorOver05SH = "yellow";
    } else {
        $backgroundColorOver05SH = "red";
    }
    ?>
    <td data-column="% Over 0.5 SH" style="background-color: <?=$backgroundColorOver05SH;?>;">
    <?php
    /* make sure to close:
    if (($row['TotalMatch']) > 9){ 
    */
    }
    ?>
    

    If the color doesn’t change maybe it is set by a different CSS rule or script?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search