首页 > 解决方案 > 如何根据给定的字符串和按下按钮时隐藏从数据库中获取信息的表行

问题描述

给出了一个 HTML 表,其中有人(在本例中为代理)显示了以下信息 Id、Name、Rank、Location 和Status。状态可以有 3 个值。在线,离线,断开连接。根据其中一个代理的状态,我想放置 3 个按钮,例如在线、离线和断开连接,并在按下按钮时隐藏具有不同值的其他行。例如,如果我按 Online,HTML 端包含状态 Offline 和 Disconnected 的表格行会消失,这也适用于其他人。我不知道实现我之前所说的,我对任何被认为可以解决我的问题的解决方案持开放态度。

<table id="main_table">
      <tr>
        <td>Id</td>
        <td>Agent Name</td>
        <td>Agent Rank</td>
        <td>Agent Location</td>
        <td>Status</td>
      </tr>

      <?php

      require 'CMS/processing/conn.php';

      $result = mysqli_query($con,"SELECT * FROM agents");

      while($info = mysqli_fetch_array($result)) {
      ?>

      <tr>
        <td><?php echo $info['id']; ?></td>
        <td><?php echo $info['agentName']; ?></td>
        <td><?php echo $info['agentRank']; ?></td>
        <td><?php echo $info['locationNames']; ?></td>
        <td><?php echo $info['agentStatus']; ?></td>
      </tr>
    <?php
    }
    ?>
</table>

标签: phphtml

解决方案


尝试使用以下逻辑:创建 3 个按钮<a>并将状态值作为 GET 传递到同一页面,然后在获取时检查它是否具有 GET

注意:这不是答案:将直接 GET 值传递给 sql 查询可能会导致一些安全问题

<a href="?status=1" class="btn">Online</a>//put the status value based on your status
<a href="?status=2" class="btn">Offline</a>//put the status value based on your status
<a href="?status=3" class="btn">Disconnected</a>//put the status value based on your status

<table id="main_table">
      <tr>
        <td>Id</td>
        <td>Agent Name</td>
        <td>Agent Rank</td>
        <td>Agent Location</td>
        <td>Status</td>
      </tr>

      <?php

      require 'CMS/processing/conn.php';
    if(isset($_GET['status']))
    {
    $result = mysqli_query($con,"SELECT * FROM agents where agentStatus='".$_GET['status']."'");
    }
    else
    {
      $result = mysqli_query($con,"SELECT * FROM agents");
    }
      while($info = mysqli_fetch_array($result)) {
      ?>

      <tr>
        <td><?php echo $info['id']; ?></td>
        <td><?php echo $info['agentName']; ?></td>
        <td><?php echo $info['agentRank']; ?></td>
        <td><?php echo $info['locationNames']; ?></td>
        <td><?php echo $info['agentStatus']; ?></td>
      </tr>
    <?php
    }
    ?>
</table>

推荐阅读