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.

Votes + Comments
Thanks I was looking for this :)

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) {
		// 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");
11 Years
Discussion Span
Last Post by Wolf_2

you should change:

<a href=\"". $page_link ."?page=1/\" title=\"First Page\">[B]<<[/B]</a>


<a href=\"". $page_link ."?page=1/\" title=\"First Page\">[B]&lt;&lt;[/B]</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


hi, i have an existing code it does what it needs to but dups all the info into one page would it be possible to use your code?

thank you


I'm not sure I understand what defines a row on a page, or the limit of rows. Are these standard HTML table rows, or what?


you really need to add a way to copy your code from the text area. Its a real mess when copying it to notepad, the gutter and half the text on your page is included in the copy. lol

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.