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:
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:
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
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
Nice work, Did exactly what I was looking for: Return an array with only the info I need. I can work out the rest!
Nice work, very intuitive, and the examples are very fine.
Thanks a lot!
thank you so much... looking for a simple yet effective pagination class
Great code.
nice work!
cool dude.. nice work
Thank you very much! Very nice work!
I really like it, one of the simplest to use and implement on the entire net.
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;
}
Great and simple class, thanks much
thanks :)
You made my life a lot easier, thank you
Hi, is there a tutorial for beginners?
I don't understand how to use this.
Yes, very fantastic. Tx for this, its a permanent part of my library. You should have a 'buy me coffee' link somewhere :)
Thanks so much for this!!
yes its really good :) thanks. samrat
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)
);
}
i use smarty to display records...bt using this, it shows only only 1st record/per page
it look like this..but pagination links are where to display??
Thank You...
So simple class. can be customized easily.
You save my time :)
thanks a lot. it helped me.
nice man ;)
Thanks.you saved my lot of time.
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.
helpful one
thanx, for this code./pankaj sing rawat here
This is what i was looking for a long time. Thank You!
Then how can i used this with ajax
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.
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!
thanks
gooood
This is a good example !Thanks.
Thank you so much for this simple class! Your code challenged me to think about how I could implement it into my current project.
Fixed. Thanks!
Line 32, that is. sorry.
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.
A bunch of code in functions, nothing related to object oriented programming
It looks terrific. One thing, the example has 7 pages but the array of page has only 5 elements? Am I missing something?
Great Stuff, thanks alot
thanks so much ,it's very useful.
Great class! Even sombody like me, not beeing one of the great gurus out there could work with it. Thanks for sharing!
Change this line:
calculate_pages(&$total_rows, $rows_per_page, &$page_num)
to:
calculate_pages($total_rows, $rows_per_page, $page_num)
this wont work..i got an error 'Fatal error: Cannot pass parameter 3 by reference'
i cant understand this code
You should use $arr[] = .. instead of array_push(). Good work anyway.
for everybody that get the ERROR - fatal error: class 'pagination' not found
At the beginning of the php class file edit <? to <?php
neo
gooood!!!!!!!!
Really,Excellent piece of code! i think it provide a new platform 4 php developer.
Great php class. Works just fine!
i think, something is missing with this.
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 );"
Really thanks!. I used for my client site.
Regards
Munir
err.. Where is the result array that is used to loop the data to display on each page?? Am I missing something?
Great class. Thanks. It was very easy to implement it to my site.
Can you give an example page using this class?
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 "";
?>
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 :)
Hi,
very nice, but there is something I'd love to comprehend from your code.
whats the @ parameter in the function calculate mean ?
It's the class instantiator function for PHP5.
It is executed when you do $foo = new bar();
can you tell me why do you have this line?
public function __construct()
{
}
Thanks a bunch, very useful.
Used this today. It's great Steve!