PHP Pagination Script

Listed under

PS_Pagination is a flexible and easy to implement PHP pagination script which will help you split large result sets over multiple pages. Click here to download the script

Update (1 July 2009): Fixed a bug. Added functionality to pass custom parameters.

Main Features

  • Ease of integration with existing PHP applications
  • Easy to customize. The main class is just 200 code lines
  • Append custom parameters to pagination links
  • Flexibility in rendering pagination links

Usage Instructions

Step One

The first step is to include the ps_pagination.php file in your script.

Step Two

Create a ps_pagination object. The ps_pagination class constructor takes five parameters:

  • MySQL connection link
  • SQL query
  • Number of records to display per page. Defaults to 10
  • Number of pagination links to display. Defaults to 5
  • Your own custom parameters you want to be appended to pagination links

The first two are compulsory while the last three are optional.

Step Three

Next, call the paginate() function. This function returns a paginated result set for the current page. You can use this result set just as you would use a normal MySQL result set.

Step Four

The final step is to display the pagination links. You can use the renderFullNav() function to generate and display the links in one go or you can use individual function calls to display each link separately.

<?php
	//Include the PS_Pagination class
	include('ps_pagination.php');
	//Connect to mysql db
	$conn = mysql_connect('localhost', 'username', 'password');
	mysql_select_db('testdb',$conn);
	$sql = 'select title from pages';
	//Create a PS_Pagination object
	$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1&param2=value2');
	//The paginate() function returns a mysql
	//result set for the current page
	$rs = $pager->paginate();
	//Loop through the result set
	while($row = mysql_fetch_assoc($rs)) {
		echo $row['title'];
	}
	//Display the navigation
	echo $pager->renderFullNav();
?>

Functions Overview

  1. renderFirst – This function displays the link to the first page
  2. renderLast – This function displays the link to the last page
  3. renderNext – This function displays link to next page
  4. renderPrevious – This function displays link to previous page
  5. renderNav – Displays the page links
  6. renderFullNav – This function displays all the pagination links in one go

Comments

Shawn Jun 20, 2007

I just wanted to drop a line and thank you for you PHP pagination script. I’ve been using one for a while now that does not limit the amount of pages shown in the nav at one time, so I’ve had to limit my queries to 20 pages or so.

I’ve searched and searched for one that does what yours does and they’ve all been extremely complicated. Yours is simple to use, understand and modify to fit my needs if necessary.

I’m not a PHP newbie, but that particular solution has alluded me for some time.

Thanks again,
Shawn

Raymond Oct 15, 2007

Very nice pagination class!

Michael Thomson Nov 24, 2007

This is bar far the best pagination script i have found. Most others require you to do the coding within the query, but because this uses an external file, it saves the amount of php coding experience you need. World class and thank you for all your time in creation. A+++++

Joel Feb 18, 2008

It works great! Make sure when passing the sql statement, you don’t have a semicolon (;) at the end! I made that mistake and it took me a while time to figure it out.

jack khan Mar 11, 2008

well what can i say pal, it does the trick basically.

fank you mate and keep posting scripts like that

dustin Mar 12, 2008

thanks alot, this is an easy to implement script, and with a little knowledge of php it can be quickly customized and integrated!

rick kuzyk Aug 16, 2009

my question is regarding param1=value1&param2=value2. I would to substitute value1 and value2 with variables. i.e. param1=$orderby&param2=rank.

How do I do that?

Jatinder Aug 19, 2009

@rick kuzyk

The custom parameters are not for passing parmetes to the SQL query but to you own PHP script.

If you want to use ORDER BY, add that to the SQL query (second parameter)

gustavo Sep 26, 2009

could I use “limit” in my query….or how to show only X number of records (instead of 100 and 50 pages, 10 and 5 pages)…????

Jatinder Sep 30, 2009

@gustavo

Sorry, you can’t use “limit” in your query. “limit” is already being used internally by the pagination class.

Post your comment
All comments are manually verified. So don't waste your time posting spam.

  Textile Help

Digg this article Bookmark on Delicious Add to your Technorati Favorites StumbleUpon Add to BlinkList Simpify This