PHP pagination class

PHP pagination class for displaying database results in pages


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:

[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

Take care.



Posted on 29.02.2016 06:05 by RonaldoRoss


Posted on 12.05.2014 03:27 by guest

// 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;

Posted on 25.07.2013 07:38 by guest

Thanks dear,quite good

Posted on 05.07.2013 12:34 by guest

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

Thank you

Posted on 13.05.2013 12:51 by guest

nice one

Posted on 20.02.2013 18:06 by guest

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

Posted on 17.01.2013 14:35 by guest

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

Posted on 16.01.2013 20:47 by guest

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

Posted on 15.01.2013 18:54 by guest

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

Posted on 05.01.2013 21:53 by guest

Great code.

Posted on 04.01.2013 06:17 by guest

nice work!

Posted on 11.07.2012 10:43 by guest

cool dude.. nice work

Posted on 07.07.2012 06:25 by guest

Thank you very much! Very nice work!

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

Posted on 15.06.2012 09:01 by guest

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;

Posted on 08.06.2012 16:42 by guest

Great and simple class, thanks much

Posted on 07.06.2012 12:51 by guest

thanks :)

Posted on 05.06.2012 22:16 by guest

You made my life a lot easier, thank you

Posted on 27.05.2012 10:24 by guest

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

Posted on 29.04.2012 03:41 by guest

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

Posted on 08.04.2012 15:06 by guest

Thanks so much for this!!

Posted on 13.03.2012 16:07 by guest

yes its really good :) thanks. samrat

Posted on 13.03.2012 14:50 by guest

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))
array_push($arr, $i)

Posted on 01.02.2012 11:41 by guest

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

Posted on 01.02.2012 10:14 by guest

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

Posted on 30.01.2012 19:15 by guest

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

Posted on 01.12.2011 05:46 by guest

thanks a lot. it helped me.

Posted on 23.11.2011 16:34 by guest

nice man ;)

Posted on 03.11.2011 09:44 by guest saved my lot of time.

Posted on 16.08.2011 04:37 by guest

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.

Posted on 04.08.2011 10:33 by guest

helpful one

Posted on 04.08.2011 06:48 by guest

thanx, for this code./pankaj sing rawat here

Posted on 03.08.2011 07:53 by guest

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

Posted on 16.07.2011 22:34 by guest

Then how can i used this with ajax

Posted on 03.06.2011 13:32 by steve

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().


Posted on 03.06.2011 12:28 by guest

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!

Posted on 23.05.2011 09:44 by guest


Posted on 10.05.2011 18:36 by guest


Posted on 06.05.2011 04:51 by guest

This is a good example !Thanks.

Posted on 04.04.2011 06:04 by guest

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

Posted on 11.02.2011 08:15 by steve

Fixed. Thanks!

Posted on 11.02.2011 08:05 by guest

Line 32, that is. sorry.

Posted on 11.02.2011 08:04 by guest

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.

Posted on 19.12.2010 00:12 by guest

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

Posted on 10.12.2010 11:28 by guest

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

Posted on 12.11.2010 19:16 by guest

Great Stuff, thanks alot

Posted on 21.10.2010 22:31 by guest

thanks so much ,it's very useful.

Posted on 06.08.2010 18:37 by guest

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

Posted on 15.07.2010 09:50 by steve

Change this line:

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


calculate_pages($total_rows, $rows_per_page, $page_num)

Posted on 15.07.2010 09:27 by guest

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

Posted on 03.07.2010 06:41 by guest

i cant understand this code

Posted on 03.06.2010 10:23 by guest

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

Posted on 26.05.2010 11:17 by guest

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

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


Posted on 05.04.2010 04:24 by guest


Posted on 08.03.2010 06:59 by guest

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

Posted on 09.01.2010 17:51 by guest

Great php class. Works just fine!

Posted on 08.11.2009 22:54 by guest

i think, something is missing with this.

Posted on 21.09.2009 07:10 by guest

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 );"

Posted on 05.09.2009 19:57 by guest

Really thanks!. I used for my client site.


Posted on 01.08.2009 02:56 by guest

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

Posted on 18.07.2009 03:16 by guest

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

Posted on 01.07.2009 18:01 by guest

Can you give an example page using this class?

Posted on 01.07.2009 17:51 by guest

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? ;)

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 "";

Posted on 01.06.2009 16:55 by ramonfincken

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 :)

Posted on 25.02.2009 04:34 by guest


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

whats the @ parameter in the function calculate mean ?

Posted on 24.02.2009 19:40 by steve

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

Posted on 24.02.2009 08:48 by guest

can you tell me why do you have this line?

public function __construct()

Posted on 25.12.2008 04:12 by guest

Thanks a bunch, very useful.

Posted on 16.11.2008 08:05 by Silvern88

Used this today. It's great Steve!