需要在php mysql表中更改tr颜色

zysjyyx4  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(252)

我有一个从mysqli表中获取数据的表,我有一个列作为'status',其中有两个值 ACTIVE 以及 DEACTIVE . 我需要在不同的bg颜色/字体颜色中停用,在不同的颜色中处于活动状态,如何通过if else语句实现这一点。至少td换了也没问题。

<?php
// Include config file
require_once 'config.php';

// Attempt select query execution
$sql = "SELECT * FROM retail";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){

        echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";
            echo "<thead>";
                echo "<tr>";
                    echo "<th>#</th>";
                    echo "<th>operator_name</th>";
                    echo "<th>zone</th>";
                    echo "<th>nas_ip</th>";
                    echo "<th>switch_name</th>";
                    echo "<th>switch_ip</th>";
                    echo "<th>switch_port</th>";
                    echo "<th>connected_port</th>";
                    echo "<th>vlan</th>";
                echo "</tr>";
            echo "</thead>";
            echo "<tbody>";
            while($row = mysqli_fetch_array($result)){
                echo "<tr>";

                    echo "<td>" . $row['id'] . "</td>";
                    echo "<td>" . $row['operator_name'] . "</td>";
                    echo "<td>" . $row['zone'] . "</td>";
                    echo "<td>" . $row['nas_ip'] . "</td>";
                    echo "<td>" . $row['switch_name'] . "</td>";
                    echo "<td>" . $row['switch_ip'] . "</td>";
                    echo "<td>" . $row['switch_port'] . "</td>";
                    echo "<td>" . $row['connected_port'] . "</td>";
                    echo "<td>" . $row['status'] . "</td>";

                    echo "<td>";

                        echo "<a href='readr.php?id=". $row['id'] ."'  title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                        echo "<a href='updater.php?id=". $row['id'] ."'  title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                        echo "<a href='deleter.php?id=". $row['id'] ."' data-toggle='modal' data-target='#exampleModalLong'  title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";

                    echo "</td>";
                echo "</tr>";
            }
            echo "</tbody>";                            
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "<p class='lead'><em>No records were found.</em></p>";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

我也在使用这个脚本,这是正确的头格式

var $table = $('table');
    $table.bootstrapTable({

      search: true,
      pagination: true,

      buttonsClass: 'primary',

      minimumCountColumns: 2,
      columns: [{
          field: 'id',
          title: 'ID',
          sortable: true,
        }, {
          field: 'operator_name',
          title: 'OPERATOR NAME',
          sortable: true,
        }, {
          field: 'zone',
          title: 'zone',
          sortable: true,
        },
        {
          field: 'nas_ip',
          title: 'nas_ip',
          sortable: true,
        },
        {
          field: 'switch_name',
          title: 'SWITCH NAME',
          sortable: true,
        },
        {
          field: 'switch_ip',
          title: 'SWITCH IP',
          sortable: true,
        },
        {
          field: 'switch_port',
          title: 'SWITCH PORT ',
          sortable: true,
        },
        {
          field: 'connected_port',
          title: 'connected_port',
          sortable: true,
        },
        {
          field: 'vlan',
          title: 'VLAN',
          sortable: true,
        },

        {
          field: 'operation',
          title: 'Action',
        },
      ],

    });
bjg7j2ky

bjg7j2ky1#

我已更改状态td(列)。添加内联样式并在if else基础上应用背景色。检查下面的代码。

<?php
  // Include config file
  require_once 'config.php';

  // Attempt select query execution
  $sql = "SELECT * FROM retail";
  if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){

    echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";
        echo "<thead>";
            echo "<tr>";
                echo "<th>#</th>";
                echo "<th>operator_name</th>";
                echo "<th>zone</th>";
                echo "<th>nas_ip</th>";
                echo "<th>switch_name</th>";
                echo "<th>switch_ip</th>";
                echo "<th>switch_port</th>";
                echo "<th>connected_port</th>";
                echo "<th>vlan</th>";
            echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";

                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['operator_name'] . "</td>";
                echo "<td>" . $row['zone'] . "</td>";
                echo "<td>" . $row['nas_ip'] . "</td>";
                echo "<td>" . $row['switch_name'] . "</td>";
                echo "<td>" . $row['switch_ip'] . "</td>";
                echo "<td>" . $row['switch_port'] . "</td>";
                echo "<td>" . $row['connected_port'] . "</td>";
                echo "<td style='color:white;background-color:".($row['status'] == 'ACTIVE'?'green':'grey').";'>" . $row['status'] . "</td>";

                echo "<td>";

                    echo "<a href='readr.php?id=". $row['id'] ."'  title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                    echo "<a href='updater.php?id=". $row['id'] ."'  title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                    echo "<a href='deleter.php?id=". $row['id'] ."' data-toggle='modal' data-target='#exampleModalLong'  title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";

                echo "</td>";
            echo "</tr>";
        }
        echo "</tbody>";                            
    echo "</table>";
    // Free result set
    mysqli_free_result($result);
} else{
    echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
   echo "ERROR: Could not able to execute $sql. " . 
   mysqli_error($link);
 }

// Close connection
 mysqli_close($link);
?>
4nkexdtk

4nkexdtk2#

所以你用的是bootstrab表插件。您需要添加行样式处理程序。
在js脚本中,您可以尝试添加。

function rowStyle(row, index) {
  var classes = ['success','danger'];

  if(row.vlan=="ACTIVE")
  return {
     classes: classes[0]
  };

  if(row.vlan=="DEACTIVE")
  return {
     classes: classes[1]
  };

  return {};
}

在php文件中:
更改:

echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";

收件人:

echo "<table data-row-style='rowStyle' class='table js-dynamitable table-bordered table-striped table-condensed'>";

更改:

echo "<th>vlan</th>";

收件人:

echo "<th data-field='vlan'>vlan</th>";

demo:httphttp://jsfiddle.net/jyjafk8v/
http://bootstrap-table.wenzhixin.net.cn/documentation/ -如果您需要有关“rowstyle”的详细信息,请搜索“rowstyle”。

相关问题