【问题标题】:Change table row color by mysql value通过mysql值更改表行颜色
【发布时间】:2017-10-08 04:46:15
【问题描述】:

在我的表中,我有一个名为 statusid 的列。如果该列的值为 1,则整行需要涂成黄色。如果值为 2,那么它需要是红色的。 0 只是默认的白色。

最好的方法是什么?

<div class="container-fluid">
     <table id="table_id" class="cell-border order-column row-border hover compact"  width="100%">
        <thead>
            <tr>
            <th width="2%">ID</th>
            <th width="6%">Debiteur</th>
            <th width="10%">Klantnaam</th>
            <th width="3%">Aantal Pallets</th>
            <th width="3%">Totaal Gewicht</th>
            <th width="30%">PB Nummers</th>
            <th width="10%">Toevoegdatum</th>
            <th width="1%">statusid</th>
        </thead>
        <tbody>
            <!-- Fetch from db -->
            <!-- Connect to db-->>
         <?php
         $conn = mysqli_connect("localhost","root","","export") or die("Error in Connection");

         $query = mysqli_query($conn, "SELECT exportid, deb_nmr, cost_name, numb_pal, tot_weight, pb_s, date, statusid FROM export_tabel");

            while ($result = mysqli_fetch_array($query)) {
                echo "<tr>
                    <td>".$result['exportid']."</td>
                    <td>".$result['deb_nmr']."</td>
                    <td>".$result['cost_name']."</td>
                    <td>".$result['numb_pal']."</td>
                    <td>".$result['tot_weight']."</td>
                    <td>".$result['pb_s']."</td>
                    <td>".$result['date']."</td>
                    <td>".$result['statusid']."</td>
                </tr>";
            }

            ?>
        </tbody>
     </table> 
   </div>

【问题讨论】:

    标签: php mysql html-table row background-color


    【解决方案1】:

    最好使用 CSS 类来完成,这样以后您可以更轻松地重新配置颜色。

    switch ((int) $result['statusid']) {
       case 1:
          $rowClass = "row-yellow";
          break;
       case 2:
          $rowClass = "row-red";
          break;
       default:
          $rowClass = "row-default";
    }
    
    echo "<tr class='".$rowClass."'>
    

    然后在你的 CSS 中,定义几个类:

    .row-yellow {
       background-color: yellow;
    }
    .row-red {
       background-color: red;
    }
    

    如果您正在使用框架并且有某种可用的视图编辑器选项,您可以创建一个可以调用的方法并使其变得更简洁。可能看起来像:

    echo "<tr class='".$view->getStatusColor($result['statusid'])."'>
    

    我只是将其作为需要考虑的问题,因为在视图逻辑中删除 switch 语句会很快变得混乱。

    【讨论】:

      【解决方案2】:

      好的,首先你的&lt;thead&gt; 没有结束&lt;/tr&gt; 的行。其次,我认为如果您为此使用数组会很有趣。另外,“最佳”可以通过多种方式定义,因此很难回答这个问题,有很多方法可以做到这一点,具体取决于您的实际需要(例如,如果您需要在多个页面中使用它)。

      $colors = ['white', 'yellow', 'red']; // or any other string, like a HEX color or a class name (which would change the code below, but do as you wish or prefer)
      while ($result = mysqli_fetch_array($query)) {
          echo "<tr style='background-color: ".$colors[$result['statusid']].";'>
              <td>".$result['exportid']."</td>
              <td>".$result['deb_nmr']."</td>
              <td>".$result['cost_name']."</td>
              <td>".$result['numb_pal']."</td>
              <td>".$result['tot_weight']."</td>
              <td>".$result['pb_s']."</td>
              <td>".$result['date']."</td>
              <td>".$result['statusid']."</td>
          </tr>";
      }
      

      【讨论】:

      • 这对我来说很好!唯一的事情是:注意:未定义的索引:在第 123 行的 /Applications/XAMPP/xamppfiles/htdocs/export/datatables.php 中。即以下代码: echo "
      • 那是因为您的列给出的数字与数组中的索引不匹配。请注意,该数组有 3 个元素,它们的索引是 0、1 和 2。因此,如果您的数据库中有任何列的 statusid 不是 0、1 或 2,那么就会出现此错误。您可以通过许多不同的方式来解决这个问题,例如为您的数组指定一个特定的索引,也就是说:[0 =&gt; 'white', 23 =&gt; 'red']。有了这个数组,那么statusid 0 代表白色,23 代表红色。您可以在 php.net 上搜索数组
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签