PHP分页原理+代码实现

PHP分页原理


  1. Mysql分页查询Limit关键字
    sql语句
    select * from table_name limit a,b;

    limit a,b 代表从第a条数据开始,查询总共b条的数据,用limit就可以轻松实现页面分页,下面是PHP分页类

使用方法

  1. 实例化Page类(可以传入两个参数pageSize和maxSize)
  2. 调用pageCaclt方法,传入当前页,返回从数据库查询的数组
  3. 调用gainLi方法,传入当前页,返回页码显示条,可直接在html中输出该变量。
<?php
class Page{
    public $mysqli;
    public $totalCount;  //数据总条数
    public $pageSize;   //每页显示条数
    public $firstPage;  //第一页
    public $endPage;    //最后一页
    public $maxSize;     //最大显示多少页(大于该数用...代替)
    public $currentPage; //当前页
    public $pageNum;
    public function __construct($pageSize=15,$maxSize=7){
        $this->pageSize=$pageSize;
        $this->maxSize=$maxSize;
        $this->mysqli=new mysqli('localhost','root','tiger','capture');
        if($this->mysqli->connect_errno){
            echo $this->mysqli->connect_error;
            exit;
        }
        $this->totalCount=$this->getAllCount();
        $this->pageNum=ceil($this->totalCount/$pageSize);
    }
    public function getAllCount(){
        $query="select count(*) from prevideo";
        $result=$this->mysqli->query($query);
        if($result->num_rows>0){
            $data=$result->fetch_array();
            $result->close();
            return $data[0];
        }
        return 0;
    }
    public function pageCaclt($currentPage){
        if($currentPage<1||$currentPage>$this->pageNum)
            $currentPage=1;
        $start=($currentPage-1)*$this->pageSize;
        $query="select * from prevideo limit {$start},{$this->pageSize}";
        $result=$this->mysqli->query($query);
        $data=array();
        while($d=$result->fetch_array()){
            $data[]=$d;
        }
        return $data;
    }
    public function gainLi($currentPage){
        if($currentPage<1||$currentPage>$this->pageNum)
            $currentPage=1;
        $pageLi="";
        if($this->pageNum<=$this->maxSize){
            $pageLi.="<div style='margin:0 auto;width:800px;height:50px;
            text-align:center;'><span>共{$this->pageNum}页</span>&nbsp;";
            for($i=1;$i<$this->pageNum;$i++){
                if($i==$currentPage)
                    $pageLi.="<span style='background:red;'>$i</span>&nbsp;&nbsp;&nbsp;";
                else
                    $pageLi.="<span><a href='?page=$i'>$i</a></span>&nbsp;&nbsp;&nbsp";
            }
            $pageLi.="</div>";
        }
        else{
            $pre=$currentPage>1?$currentPage-1:1;
            $next=$currentPage<$this->pageNum?$currentPage+1:$this->pageNum;
            $divide=($this->maxSize-1)/2;
            $left=$currentPage-$divide;
            if($left<1)
                $left=1;
            if($left+$this->maxSize>=$this->pageNum)
                $left=$this->pageNum-$this->maxSize+1;
            $right=$left+$this->maxSize-1;
            $pageLi.="<div style='margin:0 auto;width:800px;height:50px;
            text-align:center;'><span>共{$this->pageNum}页</span>&nbsp;";
            if($currentPage==1){
                $pageLi.="<span>首页</span>&nbsp;&nbsp;";
                $pageLi.="<span>上一页</span>&nbsp;&nbsp;";
            }
            else{
                $pageLi.="<span><a href='?page=1'>首页</a></span>&nbsp;&nbsp;";
                $pageLi.="<span><a href='?page={$pre}'>上一页</a></span>&nbsp;&nbsp;";
            }
            for($i=$left;$i<=$right;$i++){
                if($i==$currentPage)
                    $pageLi.="<span style='background:red;'>$i</span>&nbsp;&nbsp;";
                else
                    $pageLi.="<span><a href='?page=$i'>$i</a></span>&nbsp;&nbsp";
            }
            if($currentPage==$this->pageNum){
                $pageLi.="<span>下一页</span>&nbsp;&nbsp;";
                $pageLi.="<span>尾页</span>";
            }
            else{
                $pageLi.="<span><a href='?page={$next}'>下一页</a></span>&nbsp;&nbsp;";
                $pageLi.="<span><a href='?page={$this->pageNum}'>尾页</a></span>";
            }
            $pageLi.="</div>";
        }
        return $pageLi;
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值