PHP pagination class

PHP pagination class for displaying database results in pages

<?
class pagination
{
	public function __construct()
	{
	}
	public function calculate_pages($total_rows, $rows_per_page, $page_num)
	{
		$arr = array();
		// calculate last page
		$last_page = ceil($total_rows / $rows_per_page);
		// make sure we are within limits
		$page_num = (int) $page_num;
		if ($page_num < 1)
		{
		   $page_num = 1;
		} 
		elseif ($page_num > $last_page)
		{
		   $page_num = $last_page;
		}
		$upto = ($page_num - 1) * $rows_per_page;
		$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
		$arr['current'] = $page_num;
		if ($page_num == 1)
			$arr['previous'] = $page_num;
		else
			$arr['previous'] = $page_num - 1;
		if ($page_num == $last_page)
			$arr['next'] = $last_page;
		else
			$arr['next'] = $page_num + 1;
		$arr['last'] = $last_page;
		$arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
		$arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
		return $arr;
	}
	function get_surrounding_pages($page_num, $last_page, $next)
	{
		$arr = array();
		$show = 5; // how many boxes
		// at first
		if ($page_num == 1)
		{
			// case of 1 page only
			if ($next == $page_num) return array(1);
			for ($i = 0; $i < $show; $i++)
			{
				if ($i == $last_page) break;
				array_push($arr, $i + 1);
			}
			return $arr;
		}
		// at last
		if ($page_num == $last_page)
		{
			$start = $last_page - $show;
			if ($start < 1) $start = 0;
			for ($i = $start; $i < $last_page; $i++)
			{
				array_push($arr, $i + 1);
			}
			return $arr;
		}
		// at middle
		$start = $page_num - $show;
		if ($start < 1) $start = 0;
		for ($i = $start; $i < $page_num; $i++)
		{
			array_push($arr, $i + 1);
		}
		for ($i = ($page_num + 1); $i < ($page_num + $show); $i++)
		{
			if ($i == ($last_page + 1)) break;
			array_push($arr, $i);
		}
		return $arr;
	}
}
?>

Usage

Simple and raw. No theory, plain code. This is it, PHP pagination class, one of the most wanted snippets for web applications. How to use it: Instantiate the class (PHP5 version): $p = new pagination(); Run the first query to select the number of total rows and call the first function, with arguments the total rows number, how many rows to show per page and current page number: $arr = $p->calculate_pages(70, 10, 1); This function will call the second one in the class to get the surrounding pages of the page we are requesting. The returned result should look like this: <pre>Array ( [limit] => LIMIT 0,10 [current] => 1 [previous] => 1 [next] => 2 [last] => 7 [info] => Page (1 of 7) [pages] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) )</pre> Take care.


Comments

RonaldoRoss
Posted on 29.02.2016 06:05

nice

guest
Posted on 12.05.2014 03:27

// at middle
$start = $page_num - ceil($show / 2);
$end = $page_num + floor($show / 2 );
if ($end >= $last_page) $start = $last_page - $show;
if ($start < 0) { $start = 0; $end = $show; }
for ($i = $start; $i < $end; $i++)
{
if ($i == $last_page) break;
array_push($arr, $i + 1);
}
return $arr;

guest
Posted on 25.07.2013 07:38

Thanks dear,quite good

guest
Posted on 05.07.2013 12:34

Hi thank you for your support so far.. is that possible to upload complete working example demo for this code .. :)

Thank you

guest
Posted on 13.05.2013 12:51

nice one

guest
Posted on 20.02.2013 18:06

To fix the problems in the middle, you can utilize this code:

// at middle
$middle = floor($show / 2);
$find_start_bottom = $page_num - $middle;
$find_start_upper = $page_num + $middle;

if($find_start_bottom < 1){
if($show > $last_page){ $show = $last_page; }
for($i=1; $i $last_page){
$start = $last_page - $show;
if($start < 1){ $start = 1; }
for($i = $start; $i< $last_page; $i++){
array_push($arr, $i);
}
}else if(($find_start_bottom >= 1)&&($find_start_upper

guest
Posted on 17.01.2013 14:35

Nice work, Did exactly what I was looking for: Return an array with only the info I need. I can work out the rest!

guest
Posted on 16.01.2013 20:47

Nice work, very intuitive, and the examples are very fine.
Thanks a lot!

guest
Posted on 15.01.2013 18:54

thank you so much... looking for a simple yet effective pagination class

guest
Posted on 05.01.2013 21:53

Great code.

guest
Posted on 04.01.2013 06:17

nice work!

guest
Posted on 11.07.2012 10:43

cool dude.. nice work

guest
Posted on 07.07.2012 06:25

Thank you very much! Very nice work!

I really like it, one of the simplest to use and implement on the entire net.

guest
Posted on 15.06.2012 09:01

as to the middle code bit guest posted above... i made more changes to it as it wasnt really working too well..

// at middle
$start = $page_num - $show / 2;
if ($start < 1) $start = 0;
if ($last_page- $page_num == 1){
$arr[] = array("p"=> floor($start));
}
for ($i = $start; $i < $page_num; $i++) {
$arr[] = array("p"=>floor($i + 1));
}

for ($i = ($page_num + 1); $i < ($page_num + $show / 2 + 1); $i++) {
if ($i == ($last_page + 1)) break;
$arr[] = array("p"=>floor($i));
}

$a = array();
$i = 0;
foreach ($arr as $page){
if ($i++ < $show) $a[] = $page;
}

guest
Posted on 08.06.2012 16:42

Great and simple class, thanks much

guest
Posted on 07.06.2012 12:51

thanks :)

guest
Posted on 05.06.2012 22:16

You made my life a lot easier, thank you

guest
Posted on 27.05.2012 10:24

Hi, is there a tutorial for beginners?
I don't understand how to use this.

guest
Posted on 29.04.2012 03:41

Yes, very fantastic. Tx for this, its a permanent part of my library. You should have a 'buy me coffee' link somewhere :)

guest
Posted on 08.04.2012 15:06

Thanks so much for this!!

guest
Posted on 13.03.2012 16:07

yes its really good :) thanks. samrat

guest
Posted on 13.03.2012 14:50

Thanks for the code it serve me well. I don't know if someone post this, but if you would like the code to only show you the amount of pages specified in the variable $show and use the actual page as middle, you'll need to do this changes.

// at middle
$start = $pageNum - $show / 2;
if ($start < 1)
$start = 0;
for ($i = $start; $i < $pageNum; $i++) {
array_push($arr, $i + 1)
);
}
for ($i = ($pageNum + 1); $i < ($pageNum + $show / 2 + 1); $i++) {
if ($i == ($last_page + 1))
break;
array_push($arr, $i)
);
}

guest
Posted on 01.02.2012 11:41

i use smarty to display records...bt using this, it shows only only 1st record/per page

guest
Posted on 01.02.2012 10:14

it look like this..but pagination links are where to display??

guest
Posted on 30.01.2012 19:15

Thank You...
So simple class. can be customized easily.
You save my time :)

guest
Posted on 01.12.2011 05:46

thanks a lot. it helped me.

guest
Posted on 23.11.2011 16:34

nice man ;)

guest
Posted on 03.11.2011 09:44

Thanks.you saved my lot of time.

guest
Posted on 16.08.2011 04:37

Thanks a bunch, bro. I programmed it from scratch on a site a looong time ago and remember how much trouble it gave me. I didn't want to reverse engineer it, so here I am. I'll never use anything else for pagination again!

Seriously, thanks, you saved a lot of people a lot of time (for free). I'm shooting some good Karma your way right now.

guest
Posted on 04.08.2011 10:33

helpful one

guest
Posted on 04.08.2011 06:48

thanx, for this code./pankaj sing rawat here

guest
Posted on 03.08.2011 07:53

This is what i was looking for a long time. Thank You!

guest
Posted on 16.07.2011 22:34

Then how can i used this with ajax

steve
Posted on 03.06.2011 13:32

Not exactly. The 1st query will use the COUNT() function to get the total number of records... 1 million as you say - this is a very fast query - and then you will select the records you want. Check MySQL site for COUNT().

S.

guest
Posted on 03.06.2011 12:28

That is not a very good class for performance.

For example, you run an SQL query and retrieve the results, then you only show a few of records that you have already retrieved from the database.

So if I pulled 1 million records out of the table and only show page 1 containing 10 records, then I would have retrieved the 999,989 records for nothing!

guest
Posted on 23.05.2011 09:44

thanks

guest
Posted on 10.05.2011 18:36

gooood

guest
Posted on 06.05.2011 04:51

This is a good example !Thanks.

guest
Posted on 04.04.2011 06:04

Thank you so much for this simple class! Your code challenged me to think about how I could implement it into my current project.

steve
Posted on 11.02.2011 08:15

Fixed. Thanks!

guest
Posted on 11.02.2011 08:05

Line 32, that is. sorry.

guest
Posted on 11.02.2011 08:04

Guys, the source code is broken. All "+" signs are missing from both the download and the code on this page. For example, line 35. Please fix this, it's annoying.

guest
Posted on 19.12.2010 00:12

A bunch of code in functions, nothing related to object oriented programming

guest
Posted on 10.12.2010 11:28

It looks terrific. One thing, the example has 7 pages but the array of page has only 5 elements? Am I missing something?

guest
Posted on 12.11.2010 19:16

Great Stuff, thanks alot

guest
Posted on 21.10.2010 22:31

thanks so much ,it's very useful.

guest
Posted on 06.08.2010 18:37

Great class! Even sombody like me, not beeing one of the great gurus out there could work with it. Thanks for sharing!

steve
Posted on 15.07.2010 09:50

Change this line:

calculate_pages(&$total_rows, $rows_per_page, &$page_num)

to:

calculate_pages($total_rows, $rows_per_page, $page_num)

guest
Posted on 15.07.2010 09:27

this wont work..i got an error 'Fatal error: Cannot pass parameter 3 by reference'

guest
Posted on 03.07.2010 06:41

i cant understand this code

guest
Posted on 03.06.2010 10:23

You should use $arr[] = .. instead of array_push(). Good work anyway.

guest
Posted on 26.05.2010 11:17

for everybody that get the ERROR - fatal error: class 'pagination' not found

At the beginning of the php class file edit <? to <?php

neo

guest
Posted on 05.04.2010 04:24

gooood!!!!!!!!

guest
Posted on 08.03.2010 06:59

Really,Excellent piece of code! i think it provide a new platform 4 php developer.

guest
Posted on 09.01.2010 17:51

Great php class. Works just fine!

guest
Posted on 08.11.2009 22:54

i think, something is missing with this.

guest
Posted on 21.09.2009 07:10

test this but it give error like ERROR: "Fatal error: Cannot pass parameter 3 by reference on line $arr = $p->calculate_pages( $result, 10, 1 );"

guest
Posted on 05.09.2009 19:57

Really thanks!. I used for my client site.

Regards
Munir

guest
Posted on 01.08.2009 02:56

err.. Where is the result array that is used to loop the data to display on each page?? Am I missing something?

guest
Posted on 18.07.2009 03:16

Great class. Thanks. It was very easy to implement it to my site.

guest
Posted on 01.07.2009 18:01

Can you give an example page using this class?

guest
Posted on 01.07.2009 17:51

I tested it using this code below but I got this

ERROR: "Fatal error: Cannot pass parameter 3 by reference on line $arr = $p->calculate_pages( $result, 10, 1 );"

How to resolve this? ;)

<?php
require_once( 'classes/connection.php' );
require_once( 'classes/php-pagination-class.php' );
$p = new pagination();
$qry = mysql_query( "SELECT * FROM items" );
$result = mysql_num_rows( $qry );
$arr = $p->calculate_pages( $result, 10, 1 );
echo "";
echo print_r( $arr );
echo "";
?>

ramonfincken
Posted on 01.06.2009 16:55

Excellent piece of code!
I've altered the public function to reach for the get_surrounding_pages to set the $show from a public call :)

guest
Posted on 25.02.2009 04:34

Hi,

very nice, but there is something I'd love to comprehend from your code.

whats the @ parameter in the function calculate mean ?

steve
Posted on 24.02.2009 19:40

It's the class instantiator function for PHP5.
It is executed when you do $foo = new bar();

guest
Posted on 24.02.2009 08:48

can you tell me why do you have this line?

public function __construct()
{
}

guest
Posted on 25.12.2008 04:12

Thanks a bunch, very useful.

Silvern88
Posted on 16.11.2008 08:05

Used this today. It's great Steve!

Add your comment