2

The way I am doing my pagination now is as follow:

===============
First I do the normal query, and get all the result back.

After that I count the results, lets say 300. I do this with num_results

Now I check what page the user wants, and the limit, lets say 30,10.

Last I do the new query, with the limit selected.

===============

Using this method I do almost the same query twice, is there no other way to do this, in just one go.

2 Answers 2

9

Yes, you can use the SQL_CALC_FOUND_ROWS for exactly this purpose.

select SQL_CALC_FOUND_ROWS something from table limit 30,10

This way you can still get the amount of rows normally retrieved when NOT using the limit clause.

Sign up to request clarification or add additional context in comments.

9 Comments

Wow nice I will try this out!
@Dennis Haarbrink: I think this may introduce a chicken and egg situation. If the row count is required to calculate the pagination sets to be displayed (let's say 30 pages each with 10 items), then the row count must be returned first. At this point, the user can pick which page to view. Using SQL_CALC_FOUND_ROWS means that the row count is returned after the user has selected which page to view.
@Mike: I would assume that the number of items per page is a 'constant' and together with the total row count you can determine the number of pages. So let's say you want page#3, the offset becomes $num_pages * $records_per_page.
@Dennis Haarbrink: My thought was that until you have the total number of pages, you can't ask the user which one of those pages they want to see. So, to display the number of pages, you have to get the row count from the database and calculate the number of pages. You then ask the user which page they want, and go back to the database for that page.
@Mike: Well, usually you present the user with first page and from there let the user choose where to go. So your 'base' offset will always be zero.
|
0

I solved it using something like the following with one query - you get limit x,y+1 so a extra item in limit and interrogate it with array_slice - if it is empty there are no more items.

 <?php
/*
CREATE TABLE test ( f1 varchar(23), f2 varchar(23) );
INSERT INTO test  VALUES( 'one','testfdsfsdf');
INSERT INTO test  VALUES( 'two','ssasfsdff');
INSERT INTO test  VALUES( 'three','wewefferwr');
INSERT INTO test  VALUES( 'four','wer3rtetet');
INSERT INTO test  VALUES( 'five','sdfsfsdffsdf');
INSERT INTO test  VALUES( 'six','sdasdadasd');
INSERT INTO test  VALUES( 'seven','testfdsfsdf');
INSERT INTO test  VALUES( 'eight','ssasfsdff');
INSERT INTO test  VALUES( 'nine','wewefferwr');

*/


$servername     = "localhost";
$username       = "root";
$password       = "";
$dbname         = "eztrades";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$start=2;
$items_per_page = 3;
$n=$items_per_page+1;
$sql = "SELECT * FROM  TEST LIMIT ".$start.','.$n;
$result = $conn->query($sql);
print_r( $result);

while( $r  = $result->fetch_assoc()) {
        print_r( $r);
        $last_item = array_slice( $r , $items_per_page +1, 1 );
}
echo 'LAST ITEM::::';
print_r( $last_item );
if(empty($last_item)) {
    echo 'NO MORE ITEMS';
}


?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.