•
•
•
•
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
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.
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 class display { function pagination($rows, $per_page, $current_page, $page_link) { global $core,$C; // Create a Page Listing $this->pages = ceil($rows / $per_page); // If there's only one page, return now and don't bother if($this->pages == 1) { return; } // Pagination Prefix $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->"; $output = "Pages: "; // Should we show the FIRST PAGE link? if($current_page > 2) { $output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>"; } // Should we show the PREVIOUS PAGE link? if($current_page > 1) { $previous_page = $current_page - 1; $output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>"; } // Current Page Number $output .= "<strong>[ ". $current_page ." ]</strong>"; // Should we show the NEXT PAGE link? if($current_page < $this->pages) { $next_page = $current_page + 1; $output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>"; } // Should we show the LAST PAGE link? if($current_page < $this->pages - 1) { $output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>"; } // Return the output. return $output; } } $display = new display; echo $display->pagination("45", "15", "1", "http://mysite.com/index.php"); ?>
Comments (Newest First)
MattEvans | Posting Shark | Jun 21st, 2007
•
•
•
•
you should change:
to
and so on with all other non-tag <'s and probably do the same with > for any >'s
It'll output bad XHTML otherwise =P
<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>
<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>
It'll output bad XHTML otherwise =P
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)