, 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
hi there! diggin’ the website. I attempted signing up for your RSS and couldn’t. Just informing you! Take care!
Hi
can you please help me with this script I cant realyy get the point, i really need to do pagination on my project and think that thisis what i’m looking for, but it is not so clear for me.
Please help!!!