【问题标题】:How to make search results and pagination not disappear on click to next page?如何使搜索结果和分页在点击下一页时不会消失?
【发布时间】:2019-12-06 12:58:34
【问题描述】:

每当我点击下一页时,我的搜索结果就会消失,我的分页也会消失。我希望它们在我单击页码时都保持不变,例如,当我单击下一页时,表格数据会立即显示。现在我必须再次搜索关键字才能显示第 2 页的结果。有解决办法吗?我似乎无法弄清楚我的代码有什么问题。提前感谢您的帮助。

<?php
require_once "db.php";

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


    $search = $db->real_escape_string($_POST['search']);
    if($search != ""){
        $search = "%" . $search . "%";
    }

    $category = $_POST['category'];

    $targetpage = "search.php";  
        $limit =5;

        $query = "SELECT COUNT(*) as num FROM books WHERE(booktitle LIKE '$search') OR (author LIKE '$search') OR category = '$category'";
        $total_pages = mysqli_fetch_array(mysqli_query($db, $query));
        $total_pages = $total_pages['num'];

        $stages = 3;
        $page = mysqli_escape_string($db, $_GET['page']);
        if($page){
            $start = ($page - 1) * $limit; 
        }else{
            $start = 0; 
            }   


        // Get page data
        $query1 = "SELECT * FROM books WHERE(booktitle LIKE '$search') OR (author LIKE '$search') OR category = '$category' LIMIT $start, $limit";
        $result = mysqli_query($db, $query1);

        // Initial page num setup
        if ($page == 0){$page = 1;}
        $prev = $page - 1;  
        $next = $page + 1;  
        $lastpage = ceil($total_pages/$limit);  
        $LastPagem1 = $lastpage - 1;    
        $paginate = '';
        if($lastpage > 1)
        {       
            $paginate .= "<div class='paginate'>";
            // Previous

            if ($page > 1){
                $paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
            }else{
                $paginate.= "<span class='disabled'>Previous</span>";   }
            // Pages    
            if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                }
            }
            elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
            {
                // Beginning only hide later pages
                if($page < 1 + ($stages * 2))   
                {
                    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                    }
                    $paginate.= "...";
                    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                }
                // Middle hide some front and some back
                elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                {
                    $paginate.= "<a href='$targetpage?page=1'>1</a>";
                    $paginate.= "<a href='$targetpage?page=2'>2</a>";
                    $paginate.= "...";
                    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                    }
                    $paginate.= "...";
                    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                }
                // End only hide early pages
                else
                {
                    $paginate.= "<a href='$targetpage?page=1'>1</a>";
                    $paginate.= "<a href='$targetpage?page=2'>2</a>";
                    $paginate.= "...";
                    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
                    }
                }       
            }       
                    // Next
            if ($page < $counter - 1){ 
                $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
            }else{
                $paginate.= "<span class='disabled'>Next</span>";
                }
            $paginate.= "</div>";   
    }


    if(mysqli_num_rows($result) > 0){
        while($rows = mysqli_fetch_array($result)){
            $au = $rows['author'];
            $bt = $rows['booktitle'];
            $rev = $rows['reserved'];
            if($rev == 'N'){
                echo"<tr><td>$au</td><td>$bt</td><td>$rev</td><td><a href='reserve.php?id=".$rows['isbn']."'>Reserve</a></td></tr>\n";
            }
        }
        echo"</table>";
        echo "<center>".$paginate."</center>";
    }
}
?>

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    也许可以通过将您想要保留的所有内容保存到 Cookie 中来提供帮助

    setcookie(name, value, expire, path, domain, security);
    

    Name:用于设置cookie的名称。值:用于设置cookie的值。 Expire:用于设置过期时间 cookie 的时间戳,在该时间戳之后无法访问 cookie。 路径:用于指定服务器上的路径 cookie 将可用。域:用于指定域 cookie 可用。安全性:用于表示 只有存在安全的 HTTPS 连接时才应发送 cookie。

    从 cookie 中读取:

    $_COOKIE['name']
    

    在你的情况下,我会开始

    if(!isset($_COOKIE['result'])) {
    [load your DB results into the cookie]
    } else {
    [Display the table (or display any error message?)]
    }
    

    我们有三种不同的看法

    1. 为每个页面询问数据库(它可能会滞后于服务器)
    2. 将所有内容加载到 cookie 中,让用户保存所有这些数据(取决于用户网络速度和存储大小)
    3. 一次加载所有内容,并通过 JS 显示/隐藏您想要或不显示的页面(需要用户提供更多 RAM)

    编辑 1:

    PHP

    function searchBooks() {
        //saves into a cookie the books that match the conditions
        //(Cookie name: $_COOKIE['results'])
        if (isset($_COOKIE['results'])) {
            unset($_COOKIE['results']); //this way you make sure it won't cause any error when assigning it to the new search
            setcookie('results', null, -1, '/'); 
        }
        $sql =[...] //SQL Returns the and fetchs it...
        i = 0;
        while($rows = mysqli_fetch_array($sql)){
            $array[i]=$rows;
            $i++;
        }
        setcookie('results', $array, 85400);
    }
    
    function paginate() {
        $umbral = 10; //the max number of rows per page
        $page = $_GET['page']; //saves the current page into a variable for use it easier
        $disp = ($page-1)*$umbral // this set the displacement for this page
        for (i=$disp; (i<count($_COOKIE['results']) && i<$page*$umbral); i++) {
            echo [your stuff to show the list here];
            echo $_COOKIE['results'][i]['author'];
            echo $_COOKIE['results'][i]['book'];
            $next = $page+1;
            $prev = $page-1;
            echo "<a href=file.php?page='"+$next+"'> Next </a>";
            echo "<a href=file.php?page='"+$prev+"'> Prev </a>";
        }
    }
    

    警告

    请注意,如果 cookie 过期了,如果用户想加载下一页,会导致没有设置 cookie 的错误,所以最好在下一页加载时检查一下(可能在@ 987654325@ 函数)并处理该错误。

    【讨论】:

    • 我将按顺序实现的方法:3 -> 1 -> 2(如果内容没有太多要列出的内容,则为 3,如果内容不多但我的服务器可以处理,则为 1,如果不能,则为 3(代码在 1 和 2 中更简单))您首先必须在任何 HTML 或任何打印文本的 PHP 函数(echo、printf、...)之前声明 cookie 当用户按下指向下一页,cookie 必须已经加载(您可能应该在用户按下搜索按钮并且 DB 回复后立即加载它)。
    • 我不太明白,但我这样做了 if(!isset($_COOKIE['result'])) { setcookie('pagex', $_REQUEST['page'], time () + 86400); } ,我回显 cookie 只是为了看看我是否正在获取页面。如果我在第 1 页,它会回显第 1 页,当我单击下一步时,它仍然保持为 1,除非我再次提交表单然后它会更新。我想我有点明白饼干留在那里?但我仍然不确定如何实现这个......
    • 在cookie中你必须保存MySQL返回,所以在任何页面你都可以得到它,例如:第一页的$_COOKIE['result'][0]['author']第一排。例如,如果每页包含 10 行,则对于第二页,您必须执行 $_COOKIE['result'][10]['author'],将其用于一般目的:$pageDisplacement=$currentPage-1*10; $_COOKIE['result'][$pageDisplacement+$currentRow]['author'];因此,最终您将在任意数量的页面中按 10 x 10 列出所有项目。 (我正在编辑我的答案以添加代码示例)
    • 我要删除所有代码并只使用这些功能吗?并在 if(isset($_POST['submit'])) 内部调用它们?我应该在 echo [your list here] 中放入什么,我的表格数据?这是回声 $_COOKIE['results'][i]['author'];回显搜索的数据?
    猜你喜欢
    • 2020-12-26
    • 2023-03-05
    • 2019-10-03
    • 1970-01-01
    • 2020-02-22
    • 2017-08-31
    • 1970-01-01
    • 2017-03-25
    • 2015-10-04
    相关资源
    最近更新 更多