PHP pagination class

PHP pagination class for displaying database results in pages

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:

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

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


Take care.

Comments

Posted on 16.11.2008 02:05 by Silvern88

Used this today. It's great Steve!

Posted on 24.12.2008 22:12 by unknown user

Thanks a bunch, very useful.

Posted on 24.02.2009 02:48 by unknown user

can you tell me why do you have this line?

public function __construct()
{
}

Posted on 24.02.2009 13:40 by steve

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

Posted on 01.06.2009 11: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 01.07.2009 12:51 by unknown user

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

Posted on 01.07.2009 13:01 by unknown user

Can you give an example page using this class?

Posted on 17.07.2009 22:16 by unknown user

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

Posted on 31.07.2009 21:56 by unknown user

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

Posted on 05.09.2009 14:57 by unknown user

Really thanks!. I used for my client site.

Regards
Munir

Posted on 21.09.2009 02:10 by unknown user

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 08.11.2009 16:54 by unknown user

i think, something is missing with this.

Posted on 09.01.2010 11:51 by unknown user

Great php class. Works just fine!

Posted on 08.03.2010 00:59 by unknown user

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

Posted on 04.04.2010 23:24 by unknown user

gooood!!!!!!!!

Posted on 26.05.2010 06:17 by unknown user

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

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

neo

Posted on 03.06.2010 05:23 by unknown user

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

Posted 28 days ago by unknown user

i cant understand this code

Posted 15 days ago by unknown user

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

Posted 15 days ago by steve

Change this line:

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

to:

calculate_pages($total_rows, $rows_per_page, $page_num)



loader

progress bar