PHP 分页类

一个原生的PHP分页类(PHP pagination class)。
page-class-01.png

分页类代码

<?php
/**
 * @author   :gxggxl
 * @BlogURL  : https://gxusb.com
 * @DateTime : 2020/10/10 16:33
 * page.class.php
 */

class Page
{
    //成员属性
    private $page;//当前页
    private $maxRows;//总条数
    private $pageSize;//每页显示多少条
    private $maxPage = 0;//总页数
    private $url;//当前页面的URL地址
    private $urlParam = '';//当前页面的参数

    //成员方法
    public function __construct($maxRows, $pageSize = 5) {
        //进行初始化赋值操作
        $this->maxRows = $maxRows;
        $this->pageSize = $pageSize;
        //定义当前页
        $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
        //获取当前页面的URL地址
        $this->url = $_SERVER['PHP_SELF'];
        //获取总页数
        $this->getMaxPage();
        //验证当前页的值
        $this->checkPage();
        //调用URL参数
        $this->urlParam();
    }

    // getMaxPage 计算总页数
    private function getMaxPage() {
        //判断除数
        if ($this->pageSize < 1) {
            $this->pageSize = 1;
        }
        $this->maxPage = @ceil($this->maxRows/$this->pageSize);
    }

    //验证当前页
    private function checkPage() {
        if ($this->page > $this->maxPage) {
            $this->page = $this->maxPage;
        }
        if ($this->page < 1) {
            $this->page = 1;
        }
        if ($this->pageSize > $this->maxRows) {
            $this->pageSize = $this->maxRows;
        }
        if ($this->pageSize < 1) {
            $this->pageSize = 1;
        }
    }

    //过滤当前URL地址中的参数信息
    private function urlParam() {
        foreach ($_GET as $key => $value) {
            //判断参数值和参数名是否有效
            if ($value != '' && $key != 'page') {
                $this->urlParam .= '&' . $key . '=' . $value;
            }
        }
        // echo $this->urlParam;
    }

    /**
     * 输出页码
     *
     * @return string
     */
    public function showPage() {
        $str = '';
        $str .= '当前第' . $this->page . '页/共' . $this->maxPage . '页,共' . $this->maxRows . '条记录&nbsp;&nbsp;';
        $str .= '<a href="' . $this->url . '?page=1' . $this->urlParam . '">首页</a>&nbsp;&nbsp;';
        $str .= '<a href="' . $this->url . '?page=' . ($this->page - 1) . $this->urlParam . '">上一页</a>&nbsp;&nbsp;';
        $str .= '<a href="' . $this->url . '?page=' . ($this->page + 1) . $this->urlParam . '">下一页</a>&nbsp;&nbsp;';
        $str .= '<a href="' . $this->url . '?page=' . $this->maxPage . $this->urlParam . '">尾页</a>&nbsp;&nbsp;';
        return $str;
    }

    /**
     * 返回分页的limit条件
     *
     * @return string
     */
    public function limit() {
        $num = ($this->page - 1)*$this->pageSize;
        return $num . ',' . $this->pageSize;
    }
}

分页类使用方法

//引入分页类
include "page.class.php";
header("Content-Type:text/html;charset=utf-8");
//数据库连接资源
$link = mysqli_connect("127.0.0.1", "test", "123456", "test");
//得到结果集
$result = mysqli_query($link, "select * from crm_users");
//获取记录总条数
$total = mysqli_num_rows($result);
//设计每页显示条数
$pageSize = isset($_GET['size']) ? $_GET['size'] : 5;
//实例化分页类,$total(总条数),$pageSize(每页显示条数)
$page = new Page($total, $pageSize);
//拿到分页查询条件
$limit = $page->limit();
//sql语句
$sql = "select * from crm_users limit {$limit}";
//查询数据
$result = mysqli_query($link, $sql);
//把结果在表格中显示
echo '<style>a{text-decoration: none;}</style>';
echo '<table border="1" style="width: 760px;margin: 50px auto;">';
echo '<caption><h1>Users</h1></caption>';
echo '<tr><th>uid</th><th>username</th><th>sex</th><th>email</th><th>phone_num</th><th>create_time</th></tr>';
//从结果集中取得一行作为关联数组
while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr>';
    echo '<td>' . $row["uid"] . '</td>';
    echo '<td>' . $row["username"] . '</td>';
    echo '<td>' . $row["sex"] . '</td>';
    echo '<td>' . $row["email"] . '</td>';
    echo '<td>' . $row["phone_num"] . '</td>';
    echo '<td>' . date('Y年m月d日 H:i:s', $row["create_time"]) . '</td>';
    echo '<tr>';
}
echo '<tr><td colspan="6" style="text-align: right">' . $page->showPage() . '</td></tr>';
echo '</table>';

拓展

  • 另一种设置 url 地址的方法

    protected function getUrl(){
      //获取文件地址
      $path=$_SERVER['SCRIPT_NAME'];
      //获取主机名
      $host=$_SERVER['HTTP_HOST'];
      //获取端口号
      $port=$_SERVER['SERVER_PORT'];
      //获取协议
      $scheme=$_SERVER['REQUEST_SCHEME'];
      //获取参数
      $queryString=$_SERVER['QUERY_STRING'];
      if (strlen($queryString)) {
          //解析字符串返回数组
          parse_str($queryString,$array);
          unset($array['page']);
    
          $path=$path.'?'.http_build_query($array);
      }
      //拼接url地址
      $url=$scheme.'://'.$host.':'.$port.$path;
      //echo $url;
      //https://test.gxusb.com:443/page/demo.php?size=7&jsj=a
      return $url;
    }
      //设置url 地址
      protected function setUrl($page){
          if (strstr($this->url,'?') ){
              return $this->url.'&page='.$page;
          }else{
              return $this->url.'?page='.$page;
          }
    }