baktete miloud baktete miloud - 2 months ago 14x
MySQL Question

Change cell color based on MYSQL value

I want to change td color using if statement but somehow my code is not affecting all rows

  1. this is my code :

    require_once("../model/materiel.class.php" . "");
    $mt=new materiel();

    echo '<table id="customers2" class="table datatable table-striped">';
    echo "<thead>
    <th>Qte disponible</th>

    echo "<tbody>";
    foreach($data as $t){

    echo "<tr>";
    if ($t['qte_disponible_m'] == 0){
    echo "<td bgcolor='red'>".$t['qte_disponible_m']."</td>";
    }else if ($t['qte_disponible_m'] > $t['alert_m']){
    echo "<td bgcolor='green'>".$t['qte_disponible_m']."</td>";
    }else if ($t['qte_disponible_m'] == $t['alert_m']){
    echo "<td bgcolor='yellow'>".$t['qte_disponible_m']."</td>";

    echo "<td>".$t['alert_m']."</td>";

    echo "</tr>";
    echo "</tbody>";

    1. the problem i have see the screenshot below :

    If statement is like jumping next row



Add a class with the CSS background-color property (with !important if needed) to the TD instead of bgcolor. The bgcolor gets overwritten by the table-striped class.