根据返回的结果更改td bgcolor

I found numerous answers but none seemed to work for me. I have written the following code and I know it picks up the correct color but the td cell for the whole table is set to the color of the first number returned. I need it to reflect the different numbers that might be returned. I have tried to set the value of $backcolor="" at the end of every row iteration but I just cant seem to find the right place to do that ( or if its even possible). Here is what I have written

while ($row = $result->fetch_assoc())
{
    // $backcolor1="" ;

    if ($row["riskPostProbRate"]== '1')    { $backcolor1="green"; }
    elseif ($row["riskPostProbRate"]== '2'){ $backcolor1="green"; } 
    elseif ($row["riskPostProbRate"]== '3'){ $backcolor1="yellow"; } 
    elseif ($row["riskPostProbRate"]== '4'){ $backcolor1="orange"; } 
    elseif ($row["riskPostProbRate"]== '5'){ $backcolor1="red"; }

    echo '<tr class="$altrowcolor"><td>' . $row["riskId"]. '</td><td>'
         . $row["riskName"] . '</td><td >' . $row["riskDesc"] . '</td><td>'
         . $row["riskArea"] . '</td><td>' . $row["riskHeadline"] . '</td><td>'
         . $row["riskPreConCons"]. '</td><td bgcolor=>' . $row["riskPreConProb"] . '</td><td>'
         . $row["riskPreRate"]. '</td><td>' . $row["riskPreLevel"] . '</td><td>'
         . $row["riskRACPrevCons"] . '</td><td>' . $row["riskRACMitaCons"].'</td><td>'
         . $row["riskPostConsRate"] . '</td><td bgcolor=$backcolor1>'
         . $row["riskPostProbRate"] .'</td><td bgcolor=>' . $row["riskPostRate"]. '</td><td>'
         . $row["riskPostLevel"]. '</td><td></td></tr>'; 
}

Try this : use proper way : How to set background color to td.

Replace your bgcolor with below code :

Modified :

style=background-color: $backcolor1

if you want to do it in php i suggest to create a class in css for each color, than set the class of color you need.

example css

.red{
background-color:red;
}
.green{
background-color:green;
}

php

while($row = $result->fetch_assoc()) {
   // $backcolor1="" ;

   if ($row["riskPostProbRate"]== '1'  ){
      $backcolor1="green";
   }elseif($row["riskPostProbRate"]== '2'  ){ 
      $backcolor1="green";
   }elseif ($row["riskPostProbRate"]== '5'  ){ 
      $backcolor1="red";
   }

 echo '<tr class="$altrowcolor"><td class="'.$backcolor1.'">' . $row["riskId"]. '</td><td>' .     $row["riskName"]. '</td></tr>'; 
}

i put the class in that td beacuse the row is to long but you can insert the class wherever you want

Please use the following example. this example for change the color in odd even rows

$i=0;
while($row = $result->fetch_assoc()) {
$i++;
$bgcolor=($i%2==0)?'green':'yellow';
echo '<tr bgcolor="<?php echo $bgcolor;?>"><td></td></tr>'; 
}

this should work

<table>
<tbody>
    <?php
    while ($row = $result->fetch_assoc()) {
        $backcolor1 = "";

        if ($row["riskPostProbRate"] == '1') {
            $backcolor1 = "green";
        } elseif ($row["riskPostProbRate"] == '2') {
            $backcolor1 = "green";
        } elseif ($row["riskPostProbRate"] == '3') {
            $backcolor1 = "yellow";
        } elseif ($row["riskPostProbRate"] == '4') {
            $backcolor1 = "orange";
        } elseif ($row["riskPostProbRate"] == '5') {
            $backcolor1 = "red";
        }
        ?>
        <tr class="<?= $altrowcolor ?>">
            <td><?= $row["riskId"]; ?></td>
            <td><?= $row["riskName"] ?></td>
            <td><?= $row["riskDesc"] ?></td>
            <td><?= $row["riskArea"] ?></td>
            <td><?= $row["riskHeadline"] ?></td>
            <td><?= $row["riskPreConCons"] ?></td>
            <td bgcolor=""><?= $row["riskPreConProb"] ?></td>
            <td><?= $row["riskPreRate"] ?></td>
            <td><?= $row["riskPreLevel"] ?></td>
            <td><?= $row["riskRACPrevCons"] ?></td>
            <td><?= $row["riskRACMitaCons"] ?></td>
            <td><?= $row["riskPostConsRate"] ?></td>
            <td bgcolor="<?= $backcolor1 ?>"><?= $row["riskPostProbRate"] ?></td>
            <td bgcolor=""><?= $row["riskPostRate"] ?></td>
            <td><?= $row["riskPostLevel"] ?></td>
            <td></td>
        </tr>
        <?php
    }
    ?>
</tbody>

To add to Monty's answer, and to make it more understandable as to why the change from single to double quotes, please refer to this answer which explains thoroughly the effect of using single or double quotes.