Change table row color based on value

前端 未结 1 1363
春和景丽
春和景丽 2021-01-29 07:23

I have a table that looks something like this:

ID     |     photo      |   ident     | status
------------------------------------------------
80     |    img/pho         


        
相关标签:
1条回答
  • 2021-01-29 07:40

    You can use your status var as a class and add the colors for the classes in css like this

    .color-v {
      background-color: blue;
    }
    
    .color-x {
      background-color: green;
    }
    <table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
      <thead style= "background-color: #FFFFFF">
        <tr>
          <th>Photo</th>
          <th>Ident</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <?php
         if ($result = $link->query($query)) {
              $num_rows = 0;
              while ($row = $result->fetch_assoc()) {
                  $num_rows++;
                  echo
                  "<tr class='color-{$row['status']}'>
                  <td>{$row['photo']}</td>
                  <td>{$row['ident']}</td>
                  <td>{$row['status']}</td>
                  </tr>";
                 
              }
              /*freeresultset*/
              $result->free();
          }
        ?>
      </tbody>
    </table>

    0 讨论(0)
提交回复
热议问题