PHP pagination class for displaying database results in pages
<?php 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; } } ?>
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 07:05
nice
guest
Posted on 12.05.2014 05: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 09:38
Thanks dear,quite good
guest
Posted on 05.07.2013 14: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 14:51
nice one
guest
Posted on 20.02.2013 19: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 15: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 21:47
Nice work, very intuitive, and the examples are very fine.
Thanks a lot!
guest
Posted on 15.01.2013 19:54
thank you so much... looking for a simple yet effective pagination class
guest
Posted on 05.01.2013 22:53
Great code.
guest
Posted on 04.01.2013 07:17
nice work!
guest
Posted on 11.07.2012 12:43
cool dude.. nice work
guest
Posted on 07.07.2012 08: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 11: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 18:42
Great and simple class, thanks much
Add your comment