将搜索或过滤器应用于具有分页的表格

时间:2017-07-18 20:29:55

标签: php mysql

你好,先生,你可以帮我解决这个问题。我尝试做的是当我在搜索框中输入内容时,例如。等待它将显示每页5个未决预订(5行待处理预订)。但是当我尝试它时,它会显示所有超过10的未决预订。

这是图像 enter image description here

我尝试这样的东西......但它没有显示任何内容

$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>

1 个答案:

答案 0 :(得分:1)

  

...当我尝试时,它会显示超过10的所有待处理预订。

那是因为当您点击第2页,第3页......页面(从第1页导航后)时,(?s)数组将为空,即$_POST赢了\ t设置,这就是为什么$_POST['search']部分代码将在每次导航到第2页,第3页......页面时执行的原因。由于您未使用表单发送任何敏感数据,请在表单的else{...}属性中使用GET代替POST,如下所示:

method

并获取用户输入的数据:

<form action="..." method="get">

随后,您需要在每个分页链接中附加该搜索查询,以便在您从一个页面跳到另一个页面时搜索查询可用。

if (isset($_GET['search'])) {
    $valueToSearch = $_GET['valueToSearch'];
    ...