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
Used this today. It's great Steve!
Thanks a bunch, very useful.
can you tell me why do you have this line?
public function __construct()
{
}
It's the class instantiator function for PHP5.
It is executed when you do $foo = new bar();
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 :)
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 "";
?>
Can you give an example page using this class?
Great class. Thanks. It was very easy to implement it to my site.
err.. Where is the result array that is used to loop the data to display on each page?? Am I missing something?
Really thanks!. I used for my client site.
Regards
Munir
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 );"
i think, something is missing with this.
Great php class. Works just fine!
Really,Excellent piece of code! i think it provide a new platform 4 php developer.
gooood!!!!!!!!
for everybody that get the ERROR - fatal error: class 'pagination' not found
At the beginning of the php class file edit <? to <?php
neo
You should use $arr[] = .. instead of array_push(). Good work anyway.
i cant understand this code
this wont work..i got an error 'Fatal error: Cannot pass parameter 3 by reference'
Change this line:
calculate_pages(&$total_rows, $rows_per_page, &$page_num)
to:
calculate_pages($total_rows, $rows_per_page, $page_num)