Guest User

Pagination_Class

a guest
May 30th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2.  
  3. class Pagination {
  4.  
  5.   public $current_page;
  6.   public $per_page;
  7.   public $total_count;
  8.  
  9.   public function __construct($total_count=0, $page=1, $per_page=20) {
  10.     $this->per_page = (int) $per_page;
  11.     $this->total_count = (int) $total_count;
  12.     $this->current_page = (int) $page;
  13.     if($this->current_page < 1 || $this->current_page > $this->total_pages()) {
  14.       $this->current_page = 1;
  15.     }
  16.   }
  17.  
  18.   public function offset() {
  19.     return $this->per_page * ($this->current_page - 1);
  20.   }
  21.  
  22.   public function total_pages() {
  23.     return ceil($this->total_count / $this->per_page);
  24.   }
  25.  
  26.   public function previous_page() {
  27.     $prev = $this->current_page - 1;
  28.     return ($prev > 0) ? $prev : false;
  29.   }
  30.  
  31.   public function previous_link($url='') {
  32.     $link = '';
  33.     if($this->previous_page() != false) {
  34.       $link .= "<a href=\"{$url}?page={$this->previous_page()}\">";
  35.       $link .= "&larr; Previous</a> ";
  36.     }
  37.     return $link;
  38.   }
  39.  
  40.   public function next_page() {
  41.     $next = $this->current_page + 1;
  42.     return ($next <= $this->total_pages()) ? $next : false;
  43.   }
  44.  
  45.   public function next_link($url='') {
  46.     $link = '';
  47.     if($this->next_page() != false) {
  48.       $link .= "<a href=\"{$url}?page={$this->next_page()}\">";
  49.       $link .= "Next &rarr;</a> ";
  50.     }
  51.     return $link;
  52.   }
  53.  
  54.   public function number_links($url='', $window=2) {
  55.     $output = '';
  56.     $win = (int) $window;
  57.     $gap = false;
  58.     for($i=1; $i <= $this->total_pages(); $i++) {
  59.       if($win > 0 && $i > 1 + $win && $i < $this->total_pages() - $win && abs($i - $this->current_page) > $win) {
  60.         if(!$gap) {
  61.           $output .= "... ";
  62.           $gap = true;
  63.         }
  64.         continue;
  65.       }
  66.       $gap = false;
  67.       if($this->current_page == $i) {
  68.         $output .= "<strong>{$i}</strong> ";
  69.       } else {
  70.         $output .= "<a href=\"{$url}?page={$i}\">{$i}</a> ";
  71.       }
  72.     }
  73.     return $output;
  74.   }
  75.  
  76.   public function page_links($url="") {
  77.     $output = '';
  78.     if($this->total_pages() > 1) {
  79.       $output .= "<p class=\"pagination\">";
  80.       $output .= $this->previous_link('customers_oo.php');
  81.       $output .= $this->number_links('customers_oo.php');
  82.       $output .= $this->next_link('customers_oo.php');
  83.       $output .= "</p>";
  84.     }
  85.     return $output;
  86.   }
  87.  
  88. }
  89.  
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment