且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据值更改表行颜色

更新时间:2023-01-26 17:58:58

您可以将状态var用作类,并为CSS中的类添加颜色,像这样

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>