User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 374,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,550 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Dec 13th, 2006
Views: 8,550
The following snippet is a simple PHP Pagination Script which I wrote. As far as I know it works fine. It is meant to be used within a class, and I have wrapped in in a class in order to demonstrate it's usage.

The example given below will return something similar to the following:

Pages: [ 1 ] > >>

When there are a sufficient number of pages present, a Last Page link is added. When you navigate away from the first page, a previous page link is added. When you navigate past the second page, a first page link is added.

This code hasn't been tested all that much. It has worked fine for me, but I have only used it in limited situations and only in development. If you have any problems, please let me know.
Last edited : Feb 20th, 2008.
php Syntax | 4 stars
  1. <?php
  2.  
  3. class display {
  4. function pagination($rows, $per_page, $current_page, $page_link) {
  5. global $core,$C;
  6.  
  7. // Create a Page Listing
  8. $this->pages = ceil($rows / $per_page);
  9.  
  10. // If there's only one page, return now and don't bother
  11. if($this->pages == 1) {
  12. return;
  13. }
  14.  
  15. // Pagination Prefix
  16. $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
  17. $output = "Pages: ";
  18.  
  19. // Should we show the FIRST PAGE link?
  20. if($current_page > 2) {
  21. $output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
  22. }
  23.  
  24. // Should we show the PREVIOUS PAGE link?
  25. if($current_page > 1) {
  26. $previous_page = $current_page - 1;
  27. $output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
  28. }
  29.  
  30. // Current Page Number
  31. $output .= "<strong>[ ". $current_page ." ]</strong>";
  32.  
  33. // Should we show the NEXT PAGE link?
  34. if($current_page < $this->pages) {
  35. $next_page = $current_page + 1;
  36. $output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
  37. }
  38.  
  39. // Should we show the LAST PAGE link?
  40. if($current_page < $this->pages - 1) {
  41. $output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
  42. }
  43.  
  44. // Return the output.
  45. return $output;
  46. }
  47. }
  48.  
  49. $display = new display;
  50. echo $display->pagination("45", "15", "1", "http://mysite.com/index.php");
  51. ?>
Comments (Newest First)
MattEvans | Posting Shark | Jun 21st, 2007
you should change:
<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>
to
<a href=\"". $page_link ."?page=1/\" title=\"First Page\">&lt;&lt;</a>
and so on with all other non-tag <'s and probably do the same with &gt; for any >'s

It'll output bad XHTML otherwise =P
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:44 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC