daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 24th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Paginator {
  4.   private $listCount = 5;  
  5.   private $limit;
  6.   private $page = 1;
  7.   private $count;
  8.   private $pageCount;
  9.  
  10.   private $pageFrom;
  11.   private $pageTo;
  12.  
  13.   public function setCount($count){
  14.     if(!isset($this->count)){
  15.       $this->count = intval($count);
  16.     }
  17.   }
  18.  
  19.   public function getCount(){
  20.     return $this->count;
  21.   }
  22.  
  23.   public function setLimit($limit){
  24.     if(!isset($this->limit)){
  25.       $this->limit = intval($limit);
  26.     }
  27.   }
  28.  
  29.   public function getLimit(){
  30.     return $this->limit;
  31.   }
  32.  
  33.   public function setPage($page){
  34.    
  35.     $pageCount = $this->getPageCount();
  36.    
  37.     if($page>1){
  38.       $this->page = intval($page);
  39.  
  40.       if($pageCount<=$page){
  41.        
  42.         $this->page = intval($pageCount);
  43.       }      
  44.     }
  45.  
  46.  
  47.   }
  48.  
  49.   public function getPage(){
  50.  
  51.     return $this->page;
  52.   }
  53.  
  54.   public function getPageCount(){
  55.     if(!isset($this->pageCount)){
  56.       return $this->pageCount = ceil($this->getCount()/$this->getLimit());    
  57.     }
  58.     return $this->pageCount;      
  59.   }  
  60.  
  61.  
  62.   public function getOffset(){
  63.     if(!isset($this->offset)){
  64.       $page = $this->getPage();
  65.       $limit = $this->getLimit();
  66.       $pageCount = $this->getPageCount();
  67.      
  68.       $this->setPageFromTo($page,$limit,$pageCount);
  69.    
  70.       $this->offset = $page*$limit-$limit;    
  71.     }
  72.     return $this->offset;
  73.    
  74.   }
  75.  
  76.   public function setPageFromTo($page,$limit,$pageCount){
  77.       $pageFrom = $page-$this->listCount;
  78.       $pageTo = $page+$this->listCount-1;
  79.      
  80.       if($pageFrom<1){
  81.         $pageTo=$pageTo-$pageFrom+1;
  82.       }                  
  83.  
  84.       if($pageTo>$pageCount){
  85.         $pageFrom=$pageFrom+($pageCount-$pageTo);
  86.       }
  87.  
  88.       if($pageFrom<1){
  89.         $pageFrom=1;
  90.       }
  91.  
  92.       if($pageTo>$pageCount){
  93.         $pageTo=$pageCount;
  94.       }
  95.  
  96.       $this->pageFrom = $pageFrom;
  97.       $this->pageTo = $pageTo;      
  98.   }
  99.  
  100.  
  101.   public function getPageFrom(){
  102.     if(!isset($this->pageFrom)){
  103.       $this->getOffset();
  104.     }
  105.  
  106.     return $this->pageFrom;
  107.   }
  108.  
  109.  
  110.   public function getPageTo(){
  111.     if(!isset($this->pageTo)){
  112.       $this->getOffset();
  113.     }
  114.  
  115.     return $this->pageTo;
  116.   }
  117.    
  118.  
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top