douguwo2275 2017-07-18 20:29
浏览 317
已采纳

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

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 enter image description here

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>
  • 写回答

1条回答 默认 最新

  • douji4948 2017-07-18 20:48
    关注

    ... 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 i.e. $_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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解