PHP howto – Database paging (pagination)
OK, so it’s not quite the nuclear bomb I promised, but it’s just as much fun
Database paging, for those of you who are interested, is when you split the number of results returned by a query into smaller chunks, and then show those one page at a time. Think of how Google will display 10 results out of 4 236 735. Same thing.
The basic idea is to:
- Run your query, limited to the number of desired results
- Get the number of results that there would have been, without the limit
- Display the first set of results
- Build and display <prev> and <next> links, which, when clicked…
- Display the prev/next set of results, moved down/up by the desired amount
- Repeat 4 & 5
The following code sample is a very basic implementation of this idea. I have not checked the code, so apologies in advance if there are any bugs.
". $row['id'] ."
". $row['name'] ."
". $row['surname'] ."
";
}
// Don't bother building paging if we don't have records
if ($no_results) {
$line_output = "No records found...";
$page_output = "";
}
else {
// Build
and links and save to $page_output
$rs_prev = $row_start - $howmany; // where would prev page start, given current start less no. of records
$rs_next = $row_start + $howmany; // where would next page start, given current start plus no. of records
// If for some reason the next
starting point is negative, do not display
// This happens when our current starting point is already 0
// This may happen if some smartass manually changes the rs= bit in the url
$page_output_prev = ($rs_prev < 0) ? "" : "Previous";
// Will the next page jump start point exceed the number of records returned?
// If so, don't display '
$page_output_next = ($rs_next >= $count_result) ? "" : "Next";
// Just something to put between
& , IF they are both active
if (($page_output_prev == "") || ($page_output_next == "")) {$page_output_breaker = "";}
else { $page_output_breaker = " || ";}
// Build final paging output
$page_output = $page_output_prev . $page_output_breaker . $page_output_next;
}
// Write the outputs
echo $line_output;
echo $page_output;
?>
A few points worth taking note of:
Row counting
To get the total number of results, I have used
SELECT SQL_CALC_FOUND_ROWS
followed by a second query
SELECT FOUND_ROWS() AS total
As stated in the comments, this will return the number of results that there would have been without a limit clause. There are other ways to achieve this, namely using count() in a second query, but this way is apparently quicker, and also slightly cleaner code.
Building the paging links
In the code I have used
$_GET['rs']
What this does is get the value from the part of the URL that looks something like http://www.yoursite.com/index.php?rs=10
That value then becomes our next starting point, and is injected into the SQL query.
I’ve seen some tutorials where page numbers are used instead of starting records. This is fairly easy to achieve, and involves dividing the number of records returned by the size of the desired result set to get the number of pages, and then multiply again when determining the next starting point for the limit. I’ve not done that in this tutorial for the sake of simplicity. Besides, Google uses the records, and not pages, method. Can’t be that terrible
Extending the functionality
In this example I’m echoing the result to screen. You could instead wrap this up in a function and return the results. Another easy modification would be to alternate the background colours, as shown in my previous howto.
And that’s it for today. If you found this useful, of would like to improve it, comments are always appreciated!
Tags: database paging, howto, html, MySQL, php, programming, tutorial, web
This entry was posted
on Thursday, June 26th, 2008 at 11:06 am and is filed under php.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Thanks for this script; I’ve been struggling with pagination for several days, now, and this really helped me.
However, there is a bug: please change
“echo $paging_output;” to “echo $page_output;”, otherwise your pages don’t show up.
Thanks again,
-Janice
Aah, yes. Well spotted. I though something would crop up. All fixed. Glad I could help.
Thanks,
Denham.
I am having a bit of trouble with this. I made a database with all the right rom names ect but the page numbers and link to the next pages dont show up.
The page is http://jbvideos.malkinmedias.com/test.php
What am i doing wrong. Ps there are 2 rows in the table with data but i changed it to display 1 per page to test.
Thanks anyway
Alex Malkin
Thanks man that really helped me
It’s excellent…very nice explanation is provided by you in a simpler manner!!!
Thanks,
Manali Shah
Thanks a lot
this concept solve my problem
thanks
Syed Zafar Ali Shah
Computer Software Engineer
just taken a snap at your code. i hope this gonna help me ease pagination problem. if it wont work, i will then consult you again.
thamks
Works Perfectly! Thank you very much. This is exactly what I needed.
Glad to have helped a few people
It helped me a lot……thank you So much…..Have a good day ahead……………..
Nice one! Just type for this into google and out popped this little gem. Perfect for what I was after so a big thank you for sharing.
Great Help. Is it possible to put the output into a table format?
Thanks. I figured it out.
Nice one, works great and I’ve managed to change it to a style I like for my sites.
plese send to send complete display data base list with paging
VERY VERY NICE. VERY SIMPLE.
Works great!!! Nice work