0

I found a topic that discussed how to change an html table's row color based on a row's value. In my case, that solution didn't work. I would like to change the color of each row based on the value of the class associated with the table row. As an example: where the class of the is "red" in red, color the row red, but maintain a default color for others. What can I do to make this work?

    <table class="table table-striped" id="mytable">
      <thead>
        <tr>
          <th>Nom du cours</th>
          <th>Matière</th>
          <th>Date d'enregistrement</th>
          <th>Date d'apprentissage</th>
          <th>Nombre d'études</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php
          while($cour = $load->fetch()){
            if(condition){
                $data = "red";
            }else{
                $data = "none";
            }
            echo '
                <tr id = "cours_row" class='.$data.'>
                <td class="nom_cours">'.$cour['nom_cours'].'</td>
                <td class="nom_matiere">'.$cour['nom_matiere'].'</td>
                <td class="first_append">'.$cour['first_append'].'</td>
                <td class="next_append">'.$formater -> format(strtotime($cour['next_append'])).'</td>
                <td class="number_append">'.$cour['number_append'].'</td>
                <td class="actions">Actions</td>
            </tr>
            ';
           }
         ?>
      </tbody>
    </table>
ChrisM
  • 1,576
  • 6
  • 18
  • 29
Nathan
  • 1
  • 1

1 Answers1

0

Deciding what to assign in PHP:

$data = condition?"red":"natural";

CSS:

.red {
    background-color: red;
}
.natural {
    background-color: inherit;
}
ErTR
  • 863
  • 1
  • 14
  • 37
  • That currently one of my problem. The dynatable plugin correct automaticaly the page after CSS execution. I see the red background on the table for a milisecond but then, the color comes back to white after the page load... – Nathan Aug 29 '18 at 04:54
  • I need to see the HTML source of live page, can you provide a link to test it live? – ErTR Aug 30 '18 at 22:25