Skip to content

Instantly share code, notes, and snippets.

@AbdallaZaki
Last active December 16, 2016 16:05
Show Gist options
  • Save AbdallaZaki/773b3c3b7c33624c1c0277c68a0ae04e to your computer and use it in GitHub Desktop.
Save AbdallaZaki/773b3c3b7c33624c1c0277c68a0ae04e to your computer and use it in GitHub Desktop.
<?php
class Paging {
public function get_paging_info($row_count, $page_length = 10, $page_num = 1, $max_page_num = 5) {
$count = $row_count;
if ($count == 0) {
return "no data";
}
$page_num = $this->page_num_validation ( $page_num );
$pages_count = $this->pages_count_validation ( ($count / $page_length) );
$offset = $page_length * ($page_num - 1);
$max_page_num = $this->max_page_num_validation ( $max_page_num, $pages_count );
if ($count < $offset) {
$page_length = 10;
$offset = 0 * $page_length;
$page_num = 1;
$pages_count = 1;
}
$page_num_array = array ();
$next_page = FALSE;
$prev_page = FALSE;
if ($pages_count > 1) {
$next_page = ($pages_count >= ($page_num + 1)) ? $page_num + 1 : false;
$prev_page = (($page_num - 1) > 0) ? $page_num - 1 : false;
if ($max_page_num > 1) {
$page_num_array = $this->getnumarray ( $page_num, $max_page_num, $pages_count );
}
}
return array (
'page_num_array' => $page_num_array,
'next_page' => $next_page,
'prev_page' => $prev_page,
'offset'=>$offset,
'limit' =>$page_length
);
}
private function page_num_validation($page_num) {
return $page_num > 0 ? $page_num : 1;
}
private function pages_count_validation($pages_count) {
$intval = intval ( $pages_count );
if ($pages_count < 1) {
$pages_count = 1;
} else if ($intval < $pages_count) {
$pages_count = $intval + 1;
}
return $pages_count;
}
private function max_page_num_validation($max_page_num, $pages_count) {
if ($max_page_num < 2) {
$max_page_num = 2;
}
if ($max_page_num > $pages_count) {
$max_page_num = $pages_count;
if ($max_page_num > 5) {
$max_page_num = 5;
}
}
return $max_page_num;
}
private function getnumarray($page_num, $max_page_num, $pages_count) {
$page_num_array = array ();
$gen_counter = 1;
$last_val = $page_num;
$page_num_array [] = $page_num;
for($i = 1; $i <= $max_page_num - 1; $i ++) {
$last_val ++;
if ($last_val <= $pages_count) {
$page_num_array [] = $page_num + $i;
$gen_counter ++;
}
}
for($i = 1; $i <= $max_page_num; $i ++) {
$gen_counter ++;
if ($gen_counter <= $max_page_num) {
$page_num_array [] = $page_num - $i;
} else {
break;
}
}
sort ( $page_num_array );
return $page_num_array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment