Question

I have a simple pagination script for my search, but I'm facing some trouble.

When I change the page, the query returns to the initial definition. Something like:

Definition: Select * from a;

Search: Select * from a where a.id = 1;

And then when I go to the 2nd page the query becames that: Select * from a; again.

The code: I have a form with some fields for the search and then the script:

 $sqlCount_pre = "SELECT COUNT('*') FROM a ";
 $sqlCount_pre .= " INNER JOIN b ON a.id = b.id ";
 if( sizeof( $where ) ){
    $sqlCount_pre .= ' WHERE '.implode( ' AND ',$where );
 }

 $max = 2;
 $sqlCount = mysql_query($sqlCount_pre);
 $sqlResult = ceil(mysql_result($sqlCount, 0) / $max);
 $pg = (isset($_GET["pg"])) ? (int)$_GET["pg"] : 1 ;
 $start = ($pg - 1) * $max;
 $sqlQuery = "SELECT a.* FROM a ";
 $sqlQuery .= " INNER JOIN b ON a.id = b.id ";

 if( sizeof( $where ) ){
    $sqlQuery .= ' WHERE '.implode( ' AND ',$where );
    $sqlQuery .= " LIMIT $start, $max";
 }else{
    $sqlQuery .= " LIMIT $start, $max";
 }

$sql = mysql_query($sqlQuery) or die(mysql_error());

Then i show the result in the page. After that i put the page links, as in this code:

<?php
   if($sqlResult > 1 && $pg<= $sqlResult){
       for($i=1; $i<=$sqlResult; $i++){
           echo "<a href='?pg=$i'>$i</a> ";
       }
   }       
?>

Is there something wrong or missing in this code?

Was it helpful?

Solution

When you combine a search form with pagination, you need to embed the search parameters in the paging links.

<?php
   if($sqlResult > 1 && $pg<= $sqlResult){
       $query = $_GET;
       for($i=1; $i<=$sqlResult; $i++){
           $query['pg'] = $i;
           echo "<a href='?" . http_build_query($query) ."'>$i</a> ";
       }
   }       
?>

The above code takes everything in your current query string and adds in the applicable pg attribute when building the pagination links.

-- edit --
Combining a POST search form with pagination gets a little weird. There are a couple things you can do:

Use javascript to populate a hidden "page" field in the form when you click one of the pagination links, and resubmit the form, e.g.:

$('.pagination-link').on('click', function() {
    $('#page').val($(this).html());  //where you have a hidden field #page
    $('$searchform').submit();
});

OR
Change the form method to GET, and use the example I posted above

OTHER TIPS

is_int() doc is here

$pg = (isset($_GET["pg"]) && is_int($_GET["pg"])) ? (int)$_GET["pg"] : 1 ;

of course with this there is no reason for (int)... because we've already established $_GET["pg"] as an int, so type conversion is moot.

$pg = (isset($_GET["pg"]) && is_int($_GET["pg"]) &&  $_GET["pg"] > 0) ? $_GET["pg"] : 1 ;

this will return 1 if

$_GET["pg"]

returns a string of numbers like "42". For an alternative and is_numeric and a description and comparison of the two check out this article.

If GET returns a string

pg_escape_string($_GET["pg"])

pg_escape_string doc

Back to your original question. When you change pages the limit is not being executed. So the offset and max are not being set. take a look at the query executed when you hit the second page. Are you calculating the offset correctly?

are you only specifying $pg in

  echo "<a href='?pg=$i'>$i</a> "; 

you'd have to put in the search term as well to pass it to the $where array. The count will be 0 if you don't and that's why the implode doesn't happen.

multiple variables here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top