无法显示搜索结果的php分页 [英] php pagination for search result failing to display

查看:170
本文介绍了无法显示搜索结果的php分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在学习分页.我无法使其用于搜索结果.它会正确显示首页,并带有正确数量的链接,但是单击任何链接(甚至在页面1上)都将进入空白页面.

i've just been learning pagination. i'm having trouble getting it to work for search results. it displays the first page correctly with the right number of links but clicking on any of the links (even on the page 1) goes to a blank page.

有人可以告诉我我做错了吗

can somebody please tell me what i'm doing wrong:

以下代码适用于单击搜索按钮的情况.

The following code is for when the search button is clicked.

<?php

include('includes/connect-db.php');
include('includes/functions.php');

if (isset($_GET['searchbtn'])){
    $product=$_GET['products'];
    $status=$_GET['ticket_status'];
    $order_by=$_GET['order_by'];
    $ticket_type=$_GET['ticket_type'];

    #check if product has been selected
    if ($_GET['products'] == 'select'){
        echo '<font class="error">&nbsp Please select a product to search.</font>'; 
    }else{
        if ($status == 'All' AND $order_by == 'All' AND $ticket_type == 'All' ){
            $page_query="SELECT * FROM tickets WHERE product='$product' ORDER BY created DESC";
        }elseif ($ticket_type == 'BOX'){
            $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND pbi <> '-' ORDER BY '$order_by'    ";
        }elseif ($ticket_type == 'PVR'){
            $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND inc <> '-' ORDER BY '$order_by'    ";
        }else{
            $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' ORDER BY created DESC";
        }   
    }#end of else

    $results_per_page=3;
    $result_set=pagination($results_per_page,$page_query);

    $query=$result_set['query'];
    $pages=$result_set['pages'];

    #SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset. A resource on success or False on Error

    if (!empty($query)){
        $result = mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());
        $num_results = mysqli_num_rows($result);

        if ($num_results > 0){
            displayTicket($result);
            if ($pages > 1){
                echo '</br>';
                for($i=1;$i<=$pages;$i++){
                    echo ' <a href="?page='.$i.'">'.$i.'</a> ';
                }
            }
        }else{
            echo "&nbsp No Records found.";
        }
    }#query string is not empty
}   


?>

我已将分页代码放在单独的函数中:

i have put the pagination code in a separate function:

function pagination($per_page, $pages_query){
    include('includes/connect-db.php');
    $pagination[]=array();

    #total result count 
    $total_result=mysqli_query($db,$pages_query);

    #ceil takes a decimal number and gives the nearest whole number
    $total=mysqli_num_rows($total_result);
    $pages = ceil($total / $per_page);

    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;

    $query = " LIMIT $start,$per_page";
    $query = $pages_query.$query;
    $pagination['query'] = $query;
    $pagination['pages'] = $pages; 
    return $pagination;
}

注意:仅当我尝试使用搜索功能时,链接到其他页面的分页才会失败.我已经在一个在表格中列出信息的页面上进行了尝试,并且链接工作正常.

Note: the pagination linking to other pages only fails when i attempt it with the search feature. i have tried it on a page that lists information in a table and the links work fine.

推荐答案

在我看来,问题出在链接中的信息:localhost/test/searchProductForm.php?page=1告诉searchProductForm要显示哪个页面,但没有告诉它要显示的内容.搜索信息是.否则,您的搜索页面将不知道显示什么.尝试更改分页链接以包括原始搜索链接中的所有信息(即您的productsticket_statusorder_byticket_type参数).

It looks to me like the problem is the information in the link: localhost/test/searchProductForm.php?page=1 tells searchProductForm which page to display, but it doesn't tell it what the search information was. Without that, your search page has no idea what to display. Try changing the pagination link to include all the information that was in the original search link (i.e., your products, ticket_status, order_by, and ticket_type parameters).

这篇关于无法显示搜索结果的php分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆