SoFunction
Updated on 2025-03-09

Example of PHP paging class based on object-oriented encapsulation

This article describes the paging class based on object-oriented encapsulation in PHP. Share it for your reference, as follows:

<?php
  class Page
  {
    protected $num;//The number of pieces displayed per page    protected $total;//Total number of records    protected $pageCount;//Total page count    protected $current;//Current page number    protected $offset;//Offset    protected $limit;//Pagination page number    /**
      * Constructing method
      * @param int $total Total records
      * @param int $num Number of pieces displayed per page
      */
    public function __construct($total,$num=5)
    {
      //1. Number of pieces displayed per page      $this->num = $num;
      //2. Total records      $this->total = $total;
      //3. Total page count      $this->pageCount = ceil($total/$num);
      //4. Offset      $this->offset = ($this->current-1)*$num;
      //5.Pagination page number      $this->limit = "{$this->offset},{$this->num}";
      //6. Initialize the current page      $this->current();
    }
    /**
      * Initialize the current page
      */
    public function current(){
      $this->current = isset($_GET['page'])?$_GET['page']:'1';
      //Judge the maximum range of the current page      if ($this->current>$this->pageCount){
        $this->current = $this->pageCount;
      }
      //Judge the minimum range of the current page      if ($this->current<1){
        $this->current = 1;
      }
    }
    /**
      * Access attributes that do not have permission to access
      * @param string $key Property to access
      * @return float|int|string Returns the corresponding condition to be changed
      */
    public function __get($key){
      if ($key == "limit") {
        return $this->limit;
      }
      if ($key == "offset") {
        return $this->offset;
      }
      if ($key == "current") {
        return $this->current;
      }
    }
    /**
      * Processing paging button
      * @return string spliced ​​pagination button
      */
    public function show(){
      //Judge the initial page number      $_GET['page'] = isset($_GET['page'])?$_GET['page']:'1';
      // Assign $_GET value to the upper and lower variables      $first = $end = $prev = $next = $_GET;
      // var_dump($prev);
      //Previous page      //Judge the scope of the previous page      if ($this->current-1<1){
        $prev['page'] = 1;
      }else{
        $prev['page'] = $this->current-1;
      }
      //Next page      //Judge the scope of the next page      if ($this->current+1>$this->pageCount) {
        $next["page"] = $this->pageCount;
      }else{
        $next['page'] = $this->current+1;
      }
      /*
       front page
       $first['page'] = 1;
       //Last page
       $end['page'] = $this->pageCount;
       */
      //Split path      $url = "http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
      //Split array url address bar suffix? Pass in parameters      //http://xxx/xxx/?page=value      $prev = http_build_query($prev);
      $next = http_build_query($next);
      // $first = http_build_query($first);
      // $end = http_build_query($end);
      //The complete path of splicing      $prevpath = $url."?".$prev;
      $nextpath = $url."?".$next;
      // $firstpath = $url."?".$first;
      // $endpath = $url."?".$end;
      $str = "There are a total of{$this->total}Record There are a total of{$this->pageCount}Page ";
      $str .= "<a href='{$url}?page=1'>首Page</a> ";
      $str .= "<a href='{$prevpath}'>上一Page</a> ";
      $str .= "<a href='{$nextpath}'>下一Page</a> ";
      $str .= "<a href='{$url}?page={$this->pageCount}'>尾Page</a> ";
      return $str;
    }
  }
  //Default by yourself  $a = new Page(10);
  echo $a->show();
?>

For more information about PHP related content, please check out the topic of this site:PHP+mysql database operation tutorial》、《Summary of php+mysqli database programming skills》、《PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《Summary of PHP network programming skills"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.