【问题标题】:How can I filter through an SQL generated table using a search box?如何使用搜索框筛选 SQL 生成的表?
【发布时间】:2021-08-09 17:06:55
【问题描述】:

这是我目前所拥有的(没有做任何事情)

<input type="text" id="search" " placeholder="Search..">

<table id='table'>
    <!-- INVENTORY TABLE -->
    <?php
    $query = "SELECT * FROM StatesboroSwitchActive";
    $result = mysqli_query($db, $query);
    $count = 0;

    echo "
    <tr>
    <th></th>
    <th></th>
    <th>Name</th>
    <th>Model</th>
    <th>Serial</th>
    <th>MAC</th>
    <th>Type</th>
    <th>Notes</th>
    <th>Cisco Notes</th>
    <th>Location</th>
    </tr>";
    while ($row = mysqli_fetch_array($result)) {
        $count++;
            echo "<tr><td>" . "<button id='delete'>RMA</button>" .
            "</td><td>" . "<button id='modify'>Modify</button>" .
            "</td><td>" . $row['Name'] .
            "</td><td>" . $row['Model'] .
            "</td><td>" . $row['Serial'] .
            "</td><td>" . $row['MAC']  .
                "</td><td>" . $row['Type']  .
                "</td><td>" . $row['Notes']  .
                "</td><td>" . $row['Cisco Notes']  .
                "</td><td>" . $row['Location']  . "</td></tr>";
    }
    echo "<h3>Total: " . $count . "</h3>";
    ?>

我希望能够使用搜索框将该表格过滤到用户输入的内容中。我现在很迷茫

【问题讨论】:

  • 我会为你推荐一些javascript,例如w3schools.com/howto/howto_js_sort_table.asp
  • 如果您正在使用表格,请查看:datatables.net
  • 它与 MySQL 或 PHP 有什么关系?在我看来,您在问如何在用户界面中为 HTML 表实现过滤器
  • 你真的应该在你的 SELECT 查询中添加一个 LIMIT。

标签: php html mysql html-table


【解决方案1】:

这完全取决于您在 SQL 中的所有行名。假设您的名称、型号、序列号、MAC、类型、注释和 Cisco_Notes、位置。因此,您可以添加一个提交按钮来应用这些搜索参数。然后,您可以从 SQL 添加一些过滤器,例如

SELECT * FROM StatesboroSwitchActive 
WHERE Name LIKE '%something%' 
OR Model LIKE '%something%' 
OR Serial LIKE '%something%'
... 

其余部分无需更改。如果我错了,请纠正我。

<form action="" method="post">  
<input type="text" name="search" id="search" " placeholder="Search.."/>
<input type="submit" value="Submit" />  
</form>  
<?php
$search = '%' . mysqli_real_escape_string($db, $_REQUEST['search']) . '%';     

$query = "SELECT * FROM StatesboroSwitchActive 
WHERE Name LIKE '$search' 
OR Model LIKE '$search' 
OR Serial LIKE '$search' 
OR MAC LIKE '$search' 
OR Type LIKE '$search' 
OR Notes LIKE '$search' 
OR Cisco_Notes LIKE '$search' 
OR Location LIKE '$search'"; 

$result = mysqli_query($db, $query);
$count = 0;
echo "
    <tr>
    <th></th>
    <th></th>
    <th>Name</th>
    <th>Model</th>
    <th>Serial</th>
    <th>MAC</th>
    <th>Type</th>
    <th>Notes</th>
    <th>Cisco Notes</th>
    <th>Location</th>
    </tr>";
while ($row = mysqli_fetch_array($result)) {
        $count++;
        echo "<tr><td>" . "<button id='delete'>RMA</button>" .
            "</td><td>" . "<button id='modify'>Modify</button>" .
            "</td><td>" . $row['Name'] .
            "</td><td>" . $row['Model'] .
            "</td><td>" . $row['Serial'] .
            "</td><td>" . $row['MAC']  .
            "</td><td>" . $row['Type']  .
            "</td><td>" . $row['Notes']  .
            "</td><td>" . $row['Cisco Notes']  .
            "</td><td>" . $row['Location']  . "</td></tr>";
    }
    echo "<h3>Total: " . $count . "</h3>";

灵感来自:Creating a search form in PHP to search a database?

【讨论】:

  • 这很好用,我错过了 sql (%) 中的通配符选择器,谢谢!
猜你喜欢
  • 2019-12-01
  • 2013-07-09
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多