简体   繁体   中英

applying search or filter to table with pagination

hello kind sirs can you help me with this code. What i try to do is when i type something in the search box, ex. pending it will show the 5 pending reservation per page(5 rows of pending reservation). but when i try it, it shows all the pending reservation which is more than 10.

here is the image 在此处输入图片说明

i try something like this.. but it shows nothing

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

Here is the whole code

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

... when i try it, it shows all the pending reservation which is more than 10.

That's because when you hit 2nd, 3rd, ... pages(after navigating from the 1st page), the $_POST array would be empty ie $_POST['search'] won't be set, and that's why else{...} part of the code will get executed every time you navigate to 2nd, 3rd, ... pages. Since you're not sending any sensitive data with the form, use GET instead of POST in the method attribute of the form, like this:

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

and get the user inputted data like this:

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

Subsequently, you need to attach that search query in each of your pagination links, so that the search query would be available when you hop from page to page.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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