Q:

How to set a table cel color based on its value in PHP?

 <?php
 $res=mysqli_query($con,"select * from tab_cont");
 while($rows = mysqli_fetch_assoc($res))
 {
 echo "<tr>";
        echo    "<td>"; echo $rows["id"]; echo"</td>";
        echo    "<td>";echo $rows["date"]; echo "</td>";
        echo    "<td>";echo $rows["category"];echo "</td>";
        echo    "<td>";echo $rows["issue"];echo "</td>";
        echo    "<td>";echo $rows["cr_status"];echo "</td>";
        echo    "<td>";echo $rows["priority"];echo "</td>";
        echo    "<td>"; echo $rows["assigned"]; echo "</td>";
        echo    "<td>"; echo $rows["escalation"]; echo "</td>";
        echo    "<td>"; echo $rows["action"]; echo "</td>";
        echo    "<td>"; echo $rows["resol"]; echo "</td>";
        echo    "<td>"; ?> <a href="edit.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-success">Edit</button> <?php echo "</td>";
        echo    "<td>"; ?> <a href="delete.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-danger">Delete</button> <?php echo "</td>";

 }

 ?>

Here I want to set the color for cr_status based on its value "open:green" and "red:close".I tried foreach() code, after echo $rows["cr_status"];echo..... . But I got "unexpected foreach()" error in the browser. The code I used is:
     foreach($rows["cr_status"] as $cell) 
{
    if($cell=="OPEN")
    {

            echo '<td style="background:#FF6347">'.$cell.'</td>';

    } 
    elseif ($cell=="CLOSE") 
    {

            echo '<td style="background:#228B22">'.$cell.'</td>';
    }
}

Can anyone, Plz help me with this code?

A:

$rows["cr_status"] isn't an array, so it makes no sense to loop through it. That's the reason for your error. It's not quite clear what you thought this code was supposed to be doing.
If you just want to set the colour of the cr_status cell (and open/close are the only two statuses), then
echo "<td style='background:".($rows["cr_status"] == "OPEN" ? "#FF6347" : "#228B22")."'>".$rows["cr_status"]."</td>";

should do what you need.

