简体   繁体   English

MySQLi / PHP表过滤结果查询

[英]MySQLi / PHP tables filter results query

i am looking to filter the results once the value to search is entered, so that the table headers links only return the search result. 一旦输入要搜索的值,我希望过滤结果,以便表标题链接仅返回搜索结果。 Currently it returns the entire table. 当前,它返回整个表。 How can i do it? 我该怎么做? by changing the form from post to get? 通过更改表格的形式来获得? any ideas? 有任何想法吗?

EDIT**** I haven't explained myself very well. 编辑****我还没有很好地解释自己。 Usually the table would show 20+ records, which can be sorted by clicking the table header. 通常,该表将显示20多个记录,可通过单击表标题对其进行排序。 When the search is completed, i would like the table header to only sort the search results. 搜索完成后,我希望表标题仅对搜索结果进行排序。 IE If i searched 'Tom', and it returned 3 results, i would like the link in the header to only sort those 3 returned results **** IE浏览器如果我搜索“ Tom”,它返回了3个结果,我希望标题中的链接仅对这3个返回的结果进行排序****

Thanks. 谢谢。

<?php


include 'dbconnect.php';

if(isset($_POST['search'])) {

$valueToSearch = mysqli_real_escape_string($db, $_POST['valueToSearch']);
// search in all table columns
// using concat mysql function
$sql = "SELECT * FROM contacts 
        WHERE CONCAT(FirstName, Surname, Email, HomePhone, MobPhone) 
        LIKE '%".$valueToSearch."%'";

$query = mysqli_query($db,$sql);

} else {




$orderBy = "FirstName";
$order = "asc";

 if(!empty($_GET["orderby"])) {
$orderBy = $_GET["orderby"];
}
if(!empty($_GET["order"])) {
$order = $_GET["order"];
}

$firstOrder = "asc";
$SurOrder = "asc";
$emailOrder = "desc";

if($orderBy == "FirstName" and $order == "asc") {
    $firstOrder = "desc";
}
if($orderBy == "Surname" and $order == "asc") {
    $surOrder = "desc";
}
if($orderBy == "Email" and $order == "desc") {
    $emailOrder = "asc";
}

// Build an SQL Query
$sql = "SELECT * from contacts ORDER BY " . $orderBy . " " . $order;


// Run the Query
$query = mysqli_query($db,$sql);

}

?>



    <form action="front-contact.php" method="post">
    <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
    <input type="submit" name="search" value="Filter"><br><br>

    <form action='' method=post>
    <table border='1'>
    <tr>
    <th><a href="?orderby=FirstName&order=<?php echo $firstOrder; ?>">First Name</a></th>
    <th><a href="?orderby=Surname&order=<?php echo $surOrder; ?>">Surname</th>
    <th><a href="?orderby=Email&order=<?php echo $emailOrder; ?>">Email Address</th>
    <th>Home Number</th>
    <th>Mobile Number</th>
    </tr>

  <?php




        // Loop through each returned record and store the data in $row.. // 
    while ($row = mysqli_fetch_assoc($query)) {


    // Output the column-name of $row using the array-notation // 

        echo "<form action='' method=post>";
        echo "<tr>";
        echo "<td>".$row['FirstName']."</td>";
        echo "<td>".$row['Surname']."</td>";
        echo "<td>".$row['Email']."</td>";
        echo "<td>".$row['HomePhone']."</td>";
        echo "<td>".$row['MobPhone']."</td>";
        echo " </form>";
        echo "</tr>";

    }

       echo "</table>";


?>

That makes more sense. 这更有意义。 Thank you for clarifying. 谢谢您的澄清。 I wonder if it would keep the search results when you sort, if you populated the valueToSearch input with the search query. 我想知道是否在排序时是否将搜索查询填充到valueToSearch输入中,是否可以保留搜索结果。 Something like this: 像这样:

<input type="text" name="valueToSearch" placeholder="Value To Search" value="<?php echo isset($valueToSearch) ? $valueToSearch : '' ?>">

The input is likely being cleared when you do your initial search, so when you go to sort your table, it's re-querying your database, but because the input is empty, it's then pulling all of the database records again. 进行初始搜索时,输入可能会被清除,因此当您对表进行排序时,它会重新查询数据库,但是由于输入为空,因此它将再次拉出所有数据库记录。 That's my thought. 那是我的想法。 Let me know if it doesn't work. 让我知道它是否无效。

How to Search filter table in here 如何在这里搜索过滤器表

 <?php $servername="localhost"; $username="root"; $password=""; $dbName="multi_login"; $con=new mysqli($servername,$username,$password,$dbName); if($con->connect_error){ die("Connection failed:" . $con->connect_error ); } while($row=$result->fetch_object()){ $total=$row->salary*$row->tax; echo'<tr> <td>'.$row->id.'</td> <td>'.$row->name.'</td> <td>'.$row->sex.'</td> <td>'.$row->date.'</td> <td>'.$row->salary.'</td> <td>'.$row->tax.'</td> <td>'.$total.'</td> <td> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM