Php 对分页的表应用搜索或筛选

Php 对分页的表应用搜索或筛选,php,mysql,Php,Mysql,你好,善良的先生们,你能帮我处理这个代码吗。我尝试做的是在搜索框中键入一些内容,例如pending,它将显示每页5个pending reservation(5行pending reservation)。但当我尝试时,它会显示所有超过10个的待定预订 这是图片 我想试试这样的。。但它什么也没表现出来 $query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$

你好,善良的先生们,你能帮我处理这个代码吗。我尝试做的是在搜索框中键入一些内容,例如pending,它将显示每页5个pending reservation(5行pending reservation)。但当我尝试时,它会显示所有超过10个的待定预订

这是图片

我想试试这样的。。但它什么也没表现出来

$query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%' LIMIT " . $this_page_first_result . ',' . $results_per_page"; 
这是全部代码

 <?php
        error_reporting(E_ALL & ~E_NOTICE);
        error_reporting(E_ERROR | E_PARSE);
        session_start();

    ?>

    <?php

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "srdatabase";

        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) 
        {
            die("Connection failed: " . $conn->connect_error);
        } 

        $results_per_page = 5;
        $select= "SELECT * FROM reservations";
        $result = mysqli_query($conn, $select);
        $number_of_results = mysqli_num_rows($result);

        if(!isset($_GET['page']))
        {
            $page = 1;
        }
        else
        {
            $page = $_GET['page'];
        }

        $this_page_first_result = ($page-1)*$results_per_page;

        $sql = "SELECT * FROM reservations LIMIT " . $this_page_first_result . ',' . $results_per_page;
        $result = mysqli_query($conn, $sql);

        $number_of_pages = ceil($number_of_results/$results_per_page);


    ?>

    <div id="paging-div">
    <?php
        for($page=1;$page<=$number_of_pages;$page++)
        {
            echo '<a id="pagingLink" href="adminControl.php?page=' . $page . '">' . $page . '</a>';
        }
    ?>

    <?php
        if(isset($_POST['search']))
        {
            $valueToSearch = $_POST['valueToSearch'];
            $query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%'";
            $search_result = filterTable($query);
        }
        else
        {
            $query = "SELECT * FROM reservations";
            $search_result = filterTable($query);
        }

        function filterTable($query)
        {
            $conn = mysqli_connect("localhost", "root", "", "srdatabase");
            $filter_Result = mysqli_query($conn, $query);
            return $filter_Result;
        }
    ?>


    </div>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Admin Control</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>

    <div class="topnav" id="myTopnav">
        <a href="index.php">Home</a>
        <a href="speaker.php">Speakers</a>
        <a href="about.php">About</a>
        <a href="contact.php">Contact</a>
        <a href="reservation.php">Reservation</a>
        <a href="signOut.php" id="signOut" style="float:right">Sign Out</a>
        <a href="user.php" id="user" style="float:right; text-transform:capitalize;"><?php echo $_SESSION['firstname']; ?></a>
        <a href="signUp.php" id="signUp" style="float:right">Sign Up</a>
        <a href="signIn.php" id="signIn" style="float:right" onclick="document.getElementById('id01').style.display='block'">Sign In</a>
        <a href="adminControl.php" id="adminControl" style="float:right; width:110px;">Admin control</a>
        <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
    </div>

    <br>
    <br>
    <br>
    <br>
    <h4 style="padding-left:10px; text-align:center;">Reservation List</h4>
    <hr>

    <form action="adminControl.php" method="POST">
    <input type="text" name="valueToSearch" placeholder="type a value">
    <input type="submit" name="search" value="Filter">
    </form>
    <br>
    <br>
    <div style="overflow-x:auto;">
        <table class="reservations-table">
        <tr>
            <th class="thFirstName">First Name</th>
            <th class="thLastName">Last Name</th>
            <th class="thEmailAddress">Email Address</th>
            <th class="thContactNumber">Contact Number</th>
            <th class="thSpeaker">Speaker</th>
            <th class="thTopic">Topic</th>
            <th class="thLocation">Location</th>
            <th class="thAudience">Audience</th>
            <th class="thCount">Count</th>
            <th class="thTime">Time</th>
            <th class="thDate">Date</th>
            <th class="thAction">Reservation Date</th>
            <th class="thAction">Status</th>
            <th class="thAction">Action</th>
            <th class="thAction">Action</th>
        </tr>
         <?php while($row = mysqli_fetch_array($search_result)):?>
                    <tr>
                        <td><?php echo $row['firstname'];?></td>
                        <td><?php echo $row['lastname'];?></td>
                        <td><?php echo $row['emailaddress'];?></td>
                        <td><?php echo $row['contactnumber'];?></td>
                        <td><?php echo $row['speaker'];?></td>
                        <td><?php echo $row['topic'];?></td>
                        <td><?php echo $row['location'];?></td>
                        <td><?php echo $row['audience'];?></td>
                        <td><?php echo $row['count'];?></td>
                        <td><?php echo $row['time'];?></td>
                        <td><?php echo $row['date'];?></td>
                        <td><?php echo $row['reservationdate'];?></td>
                        <td><?php echo $row['reservationstatus'];?></td>
                    </tr>
                    <?php endwhile;?>
        </table>
        </form>
    </div>

    <?php

        $epr='';
        $msg='';
        if(isset($_GET['epr']))
        $epr=$_GET['epr'];

        if($epr=='delete')
        {
           $id=$_GET['id'];
           $delete=mysqli_query($conn, "DELETE FROM reservations WHERE id=$id");
           if($delete)
             header('location:adminControl.php');
           else
             $msg='Error :'.mysqli_error(); 
        }
    ?>

    <?php

        $epr='';
        $msg='';
        if(isset($_GET['epr']))
        $epr=$_GET['epr'];

        if($epr=='approve')
        {
           $id=$_GET['id'];
           $approve=mysqli_query($conn, "UPDATE reservations SET reservationstatus='approved' WHERE id=$id");
           header('location:adminControl.php');
        }
    ?>

    <script>
    function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
    </script>

    <script>
    function ifAdmin() 
    { 
       document.getElementById("signIn").style.display = "none";
       document.getElementById("signUp").style.display = "none";
       document.getElementById("signOut").style.display = "block";
       document.getElementById("adminControl").style.display = "block";
    }
    </script>

    <script>
    function ifNotAdmin() 
    { 
       document.getElementById("signIn").style.display = "none";
       document.getElementById("signUp").style.display = "none";
       document.getElementById("signOut").style.display = "block";
       document.getElementById("adminControl").style.display = "none";
    }
    </script>

    <script>
    function ifNotLogin() 
    { 
       document.getElementById("user").style.display = "none";
       document.getElementById("signOut").style.display = "none";
       document.getElementById("adminControl").style.display = "none";
    }
    </script>

    <?php

        if (isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == true) 
            //if login
            {
                if($_SESSION['type'] == 1)
                {
                    echo "<script type='text/javascript'>ifAdmin();</script>";  
                }
                elseif($_SESSION['type'] == 0)
                {
                    echo "<script type='text/javascript'>ifNotAdmin();</script>";
                }
            }
            //if not login
            else
            {
                echo "<script type='text/javascript'>ifNotLogin();</script>";   
            }
    ?>

    <div id="footer" class="push">Copyright 2017</div>

    </body>
    </html>

函数myFunction(){
var x=document.getElementById(“myTopnav”);
如果(x.className==“topnav”){
x、 类名+=“响应”;
}否则{
x、 className=“topnav”;
}
}
函数ifAdmin()
{ 
document.getElementById(“signIn”).style.display=“无”;
document.getElementById(“注册”).style.display=“无”;
document.getElementById(“signOut”).style.display=“block”;
document.getElementById(“adminControl”).style.display=“block”;
}
函数ifNotAdmin()
{ 
document.getElementById(“signIn”).style.display=“无”;
document.getElementById(“注册”).style.display=“无”;
document.getElementById(“signOut”).style.display=“block”;
document.getElementById(“adminControl”).style.display=“无”;
}
函数ifNotLogin()
{ 
document.getElementById(“用户”).style.display=“无”;
document.getElementById(“注销”).style.display=“无”;
document.getElementById(“adminControl”).style.display=“无”;
}
。。。当我尝试它时,它会显示所有超过10的待定预订

那是因为当你打第二,第三。。。页面(从第1页导航后,
$\u POST
数组将为空,即不会设置
$\u POST['search']
,这就是为什么
否则{…}
部分代码将在每次导航到第2、第3、。。。页。由于您没有随表单发送任何敏感数据,请在表单的
方法
属性中使用
GET
而不是
POST
,如下所示:

<form action="..." method="get">
if (isset($_GET['search'])) {
    $valueToSearch = $_GET['valueToSearch'];
    ...
随后,您需要在每个分页链接中附加该搜索查询,以便在从一页跳到另一页时可以使用该搜索查询

// your code
<?php
    for($page=1;$page<=$number_of_pages;$page++)
    {
        echo "<a id='pagingLink' href='adminControl.php?page=" . $page . "&valueToSearch=". urlencode($_GET['valueToSearch']) ."&search'>" . $page . "</a>";
    }
?>
// your code
//您的代码
//你的代码

dude它正在工作,但我想是你不小心把$search而不是$valueToSearch放到了搜索中。你在考验我吗。非常感谢。你能纠正它吗?因为其他人可能需要它。echo“@Red啊,你说得对。我已经更新了我的答案,并对代码做了足够的修改。