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.

tereško
  • 58,060
  • 25
  • 98
  • 150
greenpool
  • 553
  • 4
  • 17
  • 33
  • 1
    Also in your function `$pagination[]=array();` should be `$pagination=array();` else it defeats the purpose of initiating it. ` – Lawrence Cherone Jun 03 '12 at 04:22
  • What is the URL of this blank page? Can you get the blank page to post any information about the parameters it's receiving? – octern Jun 03 '12 at 04:22
  • @octern: when i first run the search i get: http://localhost/test/searchProductForm.php?products=Box&ticket_status=All&order_by=All&ticket_type=All&searchbtn=Go%21 when i click on page 1 or 2: http://localhost/test/searchProductForm.php?page=1 http://localhost/test/searchProductForm.php?page=2 – greenpool Jun 03 '12 at 04:37
  • Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://goo.gl/KJveJ) . Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you cannot decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a quite good PDO-related tutorial](http://goo.gl/vFWnC). – tereško Jun 03 '12 at 11:22

2 Answers2

1

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).

octern
  • 4,825
  • 21
  • 38
  • thanks. i did the following and it seems to preserve the options i choose when click on the page number but its still not displaying the results. `echo "$i ";` so now when i click page 2 for example: `http://localhost/test/searchProductForm.php?page=2&products=Box&tic??ket_status=All&order_by=All&ticket_type=All` i set echo for page, start and results per page and when i ran the search i got: page 1 start 0 per page 3 but i when i click on page 1 or the page 2 link, it doesn't echo. – greenpool Jun 03 '12 at 11:20
  • Can you get it to echo anything to the page at all? If not, then the page may be dying. Do you have it set to display errors, or have you checked the error log? If not, try adding the line `ini_set('display_errors', 1)` to the beginning of your page and see if that tells you anything. – octern Jun 03 '12 at 17:19
  • Thanks. Done that and nothing. still showing me an empty page (with my selections intact). Perhaps i need to build a simple search page and test it..? – greenpool Jun 04 '12 at 07:29
-1

I know this is an old thread, but for anyone not able to solve a similar problem with the code used above, it could be to do with the lack of session_start() at the beginning of the document.

I say this because although the code is trying to obtain the session variables passed by the ?page= &products= etc to $_GET statements but they are not being retrieved into the variables from the URL but you need to place the session_start() at the begging of your PHP document to pass the information from the URL to the strings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You do not need a session to access the `$_GET` variables...only if you want to access the `$_SESSION` variables – Bowdzone Oct 30 '14 at 10:21