PHP之简单的分页类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP之简单的分页类相关的知识,希望对你有一定的参考价值。

pageClass.php

<?php

class Page{

    private $total;//总条数
    private $size;//每页条数
    private $pageCount;//总页数
    private $currentPage;//当前页码

    private $showPages = 2;//显示的页码,当前页面左右各2,就是5页
    private $pageStart;//显示页码的开始
    private $pageEnd;//显示页码的结束

    public function __construct($total,$size,$currentPage){
        $this->total = $total;
        $this->size = $size;
        $this->pageCount=ceil($total/$size);
        $this->currentPage=$currentPage;

        $this->pageStart = $this->currentPage-$this->showPages;
        $this->pageEnd = $this->currentPage+$this->showPages;

        //如果起始页码小于1那么就设定为1
        if($this->pageStart<=1){
            //难点

            $this->pageEnd = $this->pageEnd+(1-$this->pageStart);
            $this->pageStart=1;

        }

        //如果结束页大于总页数,重置结束页=总页数
        if($this->pageEnd>=$this->pageCount){

            //难点
            if($this->pageCount<$this->showPages*2+1){
                $this->pageStart=1;
            }else{
                $this->pageStart=$this->pageStart-($this->pageEnd-$this->pageCount);
            }

            $this->pageEnd=$this->pageCount;

        }

    }

    public function Pages(){

        $str=‘<div id=page>‘;
        $str.= $this->home();
        $str.= $this->lastPage();
        $str.= $this->numPage();
        $str.= $this->nextPage();
        $str.= $this->last();
        $str.= $this->countTotal();
        $str.=‘</div>‘;
        return $str;
    }

    //首页
    private function home(){
        if($this->currentPage==1){
            $str = ‘<p>首页</p>‘;
        }else{
            $str = ‘<a href=?page=1>首页</a>‘;
        }

        return $str;
    }

    //上一页
    private function lastPage(){
        if($this->currentPage==1){
            $str = ‘<p>上一页</p>‘;
        }else{
            $str = ‘<a href=?page=‘.($this->currentPage-1).‘>上一页</a>‘;
        }

        return $str;
    }

    //中间的数字页
    private function numPage(){
        $str=‘<p class="pageRemark">... </p> ‘;

        for($i=$this->pageStart;$i<=$this->pageEnd;$i++){
            if($i==$this->currentPage){
                $str.=‘<a class="cur" href=?page=‘.$i.‘>‘.$i.‘</a>‘;
            }else{
                $str.=‘<a href=?page=‘.$i.‘>‘.$i.‘</a>‘;
            }

        }
        $str.=‘<p class="pageRemark">...</p> ‘;
        return $str;
    }

    //下一页
    private function nextPage(){
        if($this->currentPage==$this->pageCount){
            $str = ‘<p>下一页</p>‘;
        }else{
            $str = ‘<a href=?page=‘.($this->currentPage+1).‘>下一页</a>‘;
        }

        return $str;
    }

    //末页
    private function last(){
        if($this->currentPage==$this->pageCount){
            $str = ‘<p>末页</p>‘;
        }else{
            $str = ‘<a href=?page=‘.$this->pageCount.‘>末页</a>‘;
        }

        return $str;
    }

    //统计
    private function countTotal(){
        return ‘<p class="pageRemark">共<b>13</b>页<b>100</b>条数据</p>‘;
    }

}

调用 page.php

<?php
    //引入分页类
    require_once ‘pageClass.php‘;
    //链接数据库
    try{
        $db = new PDO(‘mysql:host=127.0.0.1;dbname=shop;charset=utf8;port=3306‘,‘root‘,‘root‘);

    }catch(PDOException $e){
        echo ‘mysql error:‘.$e->getMessage();
    }

    $sql = ‘select * from student‘;
    $stmt = $db->prepare($sql);
    $stmt->execute();
    //获取总条数
    $total = $stmt->rowCount();
    $size = 5;//每页条数      0,8;    8,8    16,8
    //如果不存在第一页,则默认第一页
    isset($_GET[‘page‘])?$currentPage = $_GET[‘page‘]:$currentPage=1;
    $sql.=" limit ".($currentPage-1)*$size.",$size";

    $stmt = $db->prepare($sql);
    $stmt->execute();
    //查询结果集
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    p{margin:0}
    #page{height:40px;padding:20px 0px;}
    #page a{display:block;float:left;margin-right:10px;padding:2px 12px;height:24px;border:1px #cccccc solid;background:#fff;text-decoration:none;color:#808080;font-size:12px;line-height:24px;}
    #page a:hover{color:#077ee3; border:1px #077ee3 solid;}
    #page a.cur{border:none;background:#43badb;color:#fff;}
    #page p{float:left;padding:2px 12px;font-size:12px;height:24px;line-height:24px;color:#bbb;border:1px #ccc solid;background:#fcfcfc;margin-right:8px;}
    #page p.pageRemark{border-style:none;background:none;margin-right:0px;padding:4px 0px;color:#666;}
    #page p.pageRemark b{color:red;}
    #page p.pageEllipsis{border-style:none;background:none;padding:4px 0px;color:#808080;}
    </style>

</head>
<body>

<?php

    echo ‘<table>‘;
    echo ‘<tr><th>ID</th><th>姓名</th><th>Emai</th><th>手机号</th></tr>‘;
    foreach($res as $k=>$v){
        echo ‘<tr><td>‘.$v[‘id‘].‘</td><td>‘.$v[‘name‘].‘</td><td>‘.$v[‘email‘].‘</td><td>‘.$v[‘mobile‘].‘</td></tr>‘;
    }
    echo ‘</table>‘;

$page = new Page($total,$size,$currentPage);
echo $page->Pages();

?>

</body>
</html>

以上是关于PHP之简单的分页类的主要内容,如果未能解决你的问题,请参考以下文章

PHP简单漂亮的分页类

php 分页类 兄弟连的分页类无法获取到url的参数和值。

PHP非常好用的分页类

面向对象中的分页类

二十七CI框架之自己写分页类并加载(写分页还是有难度,搞了一整天)

php 论坛里面的分页问题