Simple PHP Pagination - No Database Required

DennisP 2 Tallied Votes 3K Views Share

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.

Crakken commented: Thanks I was looking for this :) +0
<?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");
?>
MattEvans 473 Veteran Poster Team Colleague Featured Poster

you should change:

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

to

<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

serdas 17 Junior Poster

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

NutanGadakh 0 Newbie Poster

Thank you

Patiodude 0 Newbie Poster

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?

Wolf_2 0 Newbie Poster

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

pritaeas 2,130 ¯\_(ツ)_/¯ Moderator Featured Poster

Double click, then copy from the editbox.

diafol commented: I keep forgetting that myself! +0
Wolf_2 0 Newbie Poster

Lol Im sorry about that, Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.