SoFunction
Updated on 2025-04-04

Perfect php pagination class

This article has shared the specific code of the php pagination class for your reference. The specific content is as follows

<?php
  /**
     file:
     Perfect page page
   */
  class Page {
    private $total;              //Total records in the data table    private $listRows;             //The number of rows displayed per page    private $limit;              // SQL statement uses limit clause to limit the number of records to obtain    private $uri;               //Automatically obtain the request address of the url    private $pageNum;             //Total page count    private $page;              //Current page    private $config = array(
        'head' => "Record", 
        'prev' => "Previous Page", 
        'next' => "Next Page", 
        'first'=> "front page", 
        'last' => "Last Page"
      );           
    //Show content in the pagination information, you can set it by yourself through the set() method    private $listNum = 10;           //The number of pages displayed in the default page list
    /**
       Constructing method, you can set the properties of the paging class
       @param int $total Calculate the total number of records for the page
       @param int $listRows Optional, sets the number of records to be displayed on each page, the default is 25
       @param mixed $query optional, to pass parameters to the target page, it can be an array or query string format
       @param bool $ord optional, the default value is true, the page starts from the first page, and false is the last page
      */
    public function __construct($total, $listRows=25, $query="", $ord=true){
      $this->total = $total;
      $this->listRows = $listRows;
      $this->uri = $this->getUri($query);
      $this->pageNum = ceil($this->total / $this->listRows);
      /*The following judgment is used to set the current face*/
      if(!empty($_GET["page"])) {
        $page = $_GET["page"];
      }else{
        if($ord)
          $page = 1;
        else
          $page = $this->pageNum;
      }

      if($total > 0) {
        if(preg_match('/\D/', $page) ){
          $this->page = 1;
        }else{
          $this->page = $page;
        }
      }else{
        $this->page = 0;
      }
      
      $this->limit = "LIMIT ".$this->setLimit();
    }

    /**
       Used to set the information to display paging, and can perform coherent operations
       @param string $param is the subscript of the member attribute array config
       @param string $value is used to set the element value corresponding to the config subscript
       @return object Returns the object itself $this, used for inertial operations
      */
    function set($param, $value){
      if(array_key_exists($param, $this->config)){
        $this->config[$param] = $value;
      }
      return $this;
    }
    
    /* Instead of calling it directly, through this method, you can directly obtain the values ​​of private member attribute limit and page outside the object */
    function __get($args){
      if($args == "limit" || $args == "page")
        return $this->$args;
      else
        return null;
    }
    
    /**
       Output paging in the specified format
       @param int Numbers 0-7 are used as parameters, and are used to customize the output paging structure and adjust the order of the structure. The default output of all structures
       @return string paging information content
      */
    function fpage(){
      $arr = func_get_args();

      $html[0] = "<span class='p1'> common<b> {$this->total} </b>{$this->config["head"]} </span>";
      $html[1] = "&nbsp;This page <b>".$this-&gt;disnum()."</b> use&nbsp;";
      $html[2] = "&amp;nbsp;This page from &lt;b&gt;{$this-&gt;start()}-{$this-&gt;end()}&lt;/b&gt; strip&amp;nbsp;";
      $html[3] = "&amp;nbsp;&lt;b&gt;{$this-&gt;page}/{$this-&gt;pageNum}&lt;/b&gt;Page&amp;nbsp;";
      $html[4] = $this-&gt;firstprev();
      $html[5] = $this-&gt;pageList();
      $html[6] = $this-&gt;nextlast();
      $html[7] = $this-&gt;goPage();
      
      $fpage = '&lt;div style="font:12px \'\5B8B\4F53\',san-serif;"&gt;';
      if(count($arr) &lt; 1)
        $arr = array(0, 1,2,3,4,5,6,7);
      
      for($i = 0; $i &lt; count($arr); $i++)
        $fpage .= $html[$arr[$i]];
    
      $fpage .= '&lt;/div&gt;';
      return $fpage;
    }
    
    /* Private method used inside the object, */
    private function setLimit(){
      if($this-&gt;page &gt; 0)
        return ($this-&gt;page-1)*$this-&gt;listRows.", {$this-&gt;listRows}";
      else
        return 0;
    }

    /* Private method used inside the object to automatically get the current URL accessed */
    private function getUri($query){  
      $request_uri = $_SERVER["REQUEST_URI"];  
      $url = strstr($request_uri,'?') ? $request_uri : $request_uri.'?';
      
      if(is_array($query))
        $url .= http_build_query($query);
      else if($query != "")
        $url .= "&amp;".trim($query, "?&amp;");
    
      $arr = parse_url($url);

      if(isset($arr["query"])){
        parse_str($arr["query"], $arrs);
        unset($arrs["page"]);
        $url = $arr["path"].'?'.http_build_query($arrs);
      }
      
      if(strstr($url, '?')) {
        if(substr($url, -1)!='?')
          $url = $url.'&amp;';
      }else{
        $url = $url.'?';
      }
      
      return $url;
    }

    /* Private method used inside the object to get the number of records starting from the current page */
    private function start(){
      if($this-&gt;total == 0)
        return 0;
      else
        return ($this-&gt;page-1) * $this-&gt;listRows+1;
    }

    /* Private method used inside the object to get the number of records at the end of the current page */
    private function end(){
      return min($this-&gt;page * $this-&gt;listRows, $this-&gt;total);
    }

    /* Private method used inside the object to obtain operation information of the previous page and home page */
    private function firstprev(){
      if($this-&gt;page &gt; 1) {
        $str = "&amp;nbsp;&lt;a href='{$this-&gt;uri}page=1'&gt;{$this-&gt;config["first"]}&lt;/a&gt;&amp;nbsp;";
        $str .= "&lt;a href='{$this-&gt;uri}page=".($this-&gt;page-1)."'&gt;{$this-&gt;config["prev"]}&lt;/a&gt;&amp;nbsp;";    
        return $str;
      }

    }
  
    /* Private method used inside the object to obtain page list information */
    private function pageList(){
      $linkPage = "&amp;nbsp;&lt;b&gt;";
      
      $inum = floor($this-&gt;listNum/2);
      /* List in front of the current page */
      for($i = $inum; $i &gt;= 1; $i--){
        $page = $this-&gt;page-$i;

        if($page &gt;= 1)
          $linkPage .= "&lt;a href='{$this-&gt;uri}page={$page}'&gt;{$page}&lt;/a&gt;&amp;nbsp;";
      }
      /*The current page information */
      if($this-&gt;pageNum &gt; 1)
        $linkPage .= "&lt;span style='padding:1px 2px;background:#BBB;color:white'&gt;{$this-&gt;page}&lt;/span&gt;&amp;nbsp;";
      
      /* List after the current page */
      for($i=1; $i &lt;= $inum; $i++){
        $page = $this-&gt;page+$i;
        if($page &lt;= $this-&gt;pageNum)
          $linkPage .= "&lt;a href='{$this-&gt;uri}page={$page}'&gt;{$page}&lt;/a&gt;&amp;nbsp;";
        else
          break;
      }
      $linkPage .= '&lt;/b&gt;';
      return $linkPage;
    }

    /* Private method used inside the object to obtain operation information for the next page and the last page */
    private function nextlast(){
      if($this-&gt;page != $this-&gt;pageNum) {
        $str = "&amp;nbsp;&lt;a href='{$this-&gt;uri}page=".($this-&gt;page+1)."'&gt;{$this-&gt;config["next"]}&lt;/a&gt;&amp;nbsp;";
        $str .= "&amp;nbsp;&lt;a href='{$this-&gt;uri}page=".($this-&gt;pageNum)."'&gt;{$this-&gt;config["last"]}&lt;/a&gt;&amp;nbsp;";
        return $str;
      }
    }

    /* Private method used inside the object to display and process form jump page */
    private function goPage(){
        if($this-&gt;pageNum &gt; 1) {
        return '&amp;nbsp;&lt;input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(==13){var page=(&gt;'.$this-&gt;pageNum.')?'.$this-&gt;pageNum.':;location=\''.$this-&gt;uri.'page=\'+page+\'\'}" value="'.$this-&gt;page.'"&gt;&lt;input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(&gt;'.$this-&gt;pageNum.')?'.$this-&gt;pageNum.':;location=\''.$this-&gt;uri.'page=\'+page+\'\'"&gt;&amp;nbsp;';
      }
    }

    /* A private method used inside the object to obtain the number of records displayed on this page */
    private function disnum(){
      if($this-&gt;total &gt; 0){
        return $this-&gt;end()-$this-&gt;start()+1;
      }else{
        return 0;
      }
    }
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.