baktete miloud baktete miloud - 4 months ago 19
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();
    $data=$mt->afficher_tous1();

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

    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>";
    echo"</table>";



    1. the problem i have see the screenshot below :



    If statement is like jumping next row



Screenshot

Answer

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.

Comments