A simple and easy to use php paging class

  • 2020-10-31 21:40:01
  • OfStack


class Page {
    //  The number of pages per page displayed in the pagination bar 
    public $rollPage = 6;
    //  The parameters to take when the page jumps 
    public $parameter  ;
    //  The default list displays the number of rows per page 
    public $listRows = 20;
    //  Starting line number 
    public $firstRow ;
    //  Total number of paginated pages 
    protected $totalPages  ;
    //  Total number of rows 
    protected $totalRows  ;
    //  The current number of pages 
    protected $nowPage    ;
    //  The total number of pages of paginated columns 
    protected $coolPages   ;
    //  Paging display customization 
    protected $config  = array(
     'redirect'=>false,
     'header'=>' records ',
     'prev'=>' on 1 page ',
     'next'=>' Under the 1 page ',
     'first'=>'1',
     'last'=>' The last 1 page ',
     'theme'=>' <div class="part1">%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage%</div> <div class="part2"> A total of   %totalPage%  page ');
    //  The default paging variable name 
    protected $varPage;
    /**
     +----------------------------------------------------------
     *  Architecture function 
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows   Total record number 
     * @param array $listRows   Number of records per page 
     * @param array $parameter   Parameters for a page break 
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$parameter='') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     // Total number of pages 
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        //$_GET validation 
        $this->nowPage = intval($_GET[$this->varPage]);
        $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 1;

        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
    /**
     +----------------------------------------------------------
     *  Paging display output 
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {

     if(0 == $this->totalRows) return '';

     // Processing parameters 
        $p = $this->varPage;
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }

        /*  Paging logic   */       

        // When the total is less than the number of pages displayed 
        if ($this->totalPages <= $this->rollPage) {
         $start = 1;
         $end   = $this->totalPages;
        }
        else{
         //
         if  ($this->nowPage <= $this->rollPage - 1) {
          $start = 1;
          $end   = $this->rollPage;

          $islast = true;
         }  
         else if ($this->nowPage > $this->totalPages - $this->rollPage + 1) {
          $start = $this->totalPages - ($this->rollPage - 1);
          $end   = $this->totalPages;

          $isfirst = true;
         }
         else{
          // Floating number 
          $size = floor($this->rollPage / 2);

          $start = $this->nowPage - $size;
          $end   = $this->nowPage + $size;

          $isfirst = true;
          $islast = true;
         }
        }

        // Turn the page string up and down 
        $upRow   = $this->nowPage - 1;
        $downRow = $this->nowPage + 1;

        
        /*  assemble HTML */

        //< 1...     ...last >
        if ($isfirst){
         $theFirst = "<a class='firstPage' href='".$url."&".$p."=1' >".$this->config['first']."</a>";
        }
        if ($islast){
         $theEnd = "<a class='lastPage' href='".$url."&".$p."=$this->totalPages' >".$this->config['last']."</a>";
        }

        if ($upRow > 0){
         $upPage = "<a class='upPage' href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
        }

        if ($downRow <= $this->totalPages){
         $downPage = "<a class='downPage' href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
        }

       if($start==3){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a>";
        }
        if($start>=4){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a>&nbsp;<span class='noEndClass'>...</span>";
        }
        //1 2 3 4 5
        for($i=$start;$i<=$end;$i++){
         if($i!=$this->nowPage){
          $linkPage .= "&nbsp;<a href='".$url."&".$p."=$i'>&nbsp;".$i."&nbsp;</a>";
         }else{
          $linkPage .= "&nbsp;<span class='current'>".$i."</span>";
         }
         if($i==$end){
          if($i<$this->totalRows){
           $linkPage .= "&nbsp;<span class='noEndClass'>...</span>";
          }
         }
        }

        $pageStr = str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage,$downPage,$theEnd),$this->config['theme']);
        // Display mode    ordinary false  Take a jump ture
        if (!empty($this->config['redirect'])){
         $html = $pageStr;

        }else{
         // Passing parameters 
         if($this->totalPages > 1){
          $redirect = "  To the first  <form method='get' action=''><input name=".$p." type='text' class='page_text' size='3' maxlength='3' value='" . $this->nowPage ."'/>  page  <input type='submit' class='page_btn' value=' determine ' />";
          if ($params){
           foreach($params as $k => $v){
            $string .= "<input type='hidden' name='" . $k . "' value='" . $v . "'/>";
           }
           $redirect = $redirect . $string . '</form></div>';
          }else{
           $redirect = $redirect . '</form></div>';
          }
         }
         // generate Html string 
         $html = $pageStr . $redirect;       
        }  
        return $html;
    }
}


Related articles: