欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP分页类的定义与用法详解

程序员文章站 2022-03-28 23:21:33
...
这篇文章主要介绍了PHP实现的分页类定义与用法,结合具体实例形式分析了php封装的分页类常用操作及具体使用技巧,需要的朋友可以参考下

具体如下:

<?php
  class Page {
     private $total;   //总记录
     private $pagesize;  //每页显示多少条
     private $limit;     //limit
     private $page;      //当前页码
     private $pagenum;   //总页码
     private $url;      //地址
     private $bothnum;   //两边保持数字分页的量
   //构造方法初始化
   public function __construct($_total, $_pagesize) {
     $this->total = $_total ? $_total : 1;
     $this->pagesize = $_pagesize;
     $this->pagenum = ceil($this->total / $this->pagesize);
     $this->page = $this->setPage();
     $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
     $this->url = $this->setUrl();
     $this->bothnum = 2;
   }
   //拦截器
   private function __get($_key) {
     return $this->$_key;
   }
   //获取当前页码
   private function setPage() {
     if (!empty($_GET['page'])) {
        if ($_GET['page'] > 0) {
          if ($_GET['page'] > $this->pagenum) {
             return $this->pagenum;
          } else {
             return $_GET['page'];
          }
        } else {
          return 1;
        }
     } else {
        return 1;
     }
   }
   //获取地址
   private function setUrl() {
     $_url = $_SERVER["REQUEST_URI"];
     $_par = parse_url($_url);
     if (isset($_par['query'])) {
        parse_str($_par['query'],$_query);
        unset($_query['page']);
        $_url = $_par['path'].'?'.http_build_query($_query);
     }
     return $_url;
   }   //数字目录
   private function pageList() {
     for ($i=$this->bothnum;$i>=1;$i--) {
      $_page = $this->page-$i;
      if ($_page < 1) continue;
        $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'" rel="external nofollow" rel="external nofollow" >'.$_page.'</a> ';
     }
     $_pagelist .= ' <span class="me">'.$this->page.'</span> ';
     for ($i=1;$i<=$this->bothnum;$i++) {
      $_page = $this->page+$i;
        if ($_page > $this->pagenum) break;
        $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'" rel="external nofollow" rel="external nofollow" >'.$_page.'</a> ';
     }
     return $_pagelist;
   }
   //首页
   private function first() {
     if ($this->page > $this->bothnum+1) {
        return ' <a href="'.$this->url.'" rel="external nofollow" >1</a> ...';
     }
   }
   //上一页
   private function prev() {
     if ($this->page == 1) {
        return '<span class="disabled">上一页</span>';
     }
     return ' <a href="'.$this->url.'&page='.($this->page-1).'" rel="external nofollow" >上一页</a> ';
   }
   //下一页
   private function next() {
     if ($this->page == $this->pagenum) {
        return '<span class="disabled">下一页</span>';
     }
     return ' <a href="'.$this->url.'&page='.($this->page+1).'" rel="external nofollow" >下一页</a> ';
   }
   //尾页
   private function last() {
     if ($this->pagenum - $this->page > $this->bothnum) {
        return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'" rel="external nofollow" >'.$this->pagenum.'</a> ';
     }
   }
   //分页信息
   public function showpage() {
     $_page .= $this->first();
     $_page .= $this->pageList();
     $_page .= $this->last();
     $_page .= $this->prev();
     $_page .= $this->next();
     return $_page;
   }
 }
?>

分页样式如下图所示:

PHP分页类的定义与用法详解

使用说明:

<?php
  $_page = new Page($_total,$_pagesize);
  //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>

相关推荐:

实例详解php分页类

TP5-分页类中的each函数

php基础分页类代码详解

以上就是PHP分页类的定义与用法详解的详细内容,更多请关注其它相关文章!

相关标签: php 详解 用法