2018-06-23 17:59:03  665684 253

一个简单实用的分页类

 标签:   


一个还算是比较简单实用美观大方的分页类


首先是分页类 page.php

<?php

class Page{
    
    private $cur;
    
    private $showPages = 2;
    
    private $pageIndex;
    
    private $pageEnd;
    
    private $total;
    
    private $size;
    
    public function __construct($cur,$total,$size){
        $this->cur=$cur;
        $this->total=$total;
        $this->size = $size;
        $this->pageCount = ceil($total/$size);
        $this->setIndexEnd();
    }
    
    private function setIndexEnd(){
        $this->pageIndex = $this->cur-$this->showPages;
        $this->pageEnd = $this->cur+$this->showPages;
        //0             pageend    +1
        //-1            pageend   +2
        if($this->pageIndex<=0){
            $this->pageEnd = $this->pageEnd+(1-$this->pageIndex);
            $this->pageIndex=1;
        }
        //14   13   -1
        //15  12    -2
        
        if($this->pageCount>($this->showPages*2+1)){
            if($this->pageEnd>$this->pageCount){

                $this->pageIndex = $this->pageIndex-($this->pageEnd-$this->pageCount);
                $this->pageEnd = $this->pageCount;
            }
        }else{
            if($this->pageEnd>$this->pageCount){
                $this->pageEnd = $this->pageCount;
            }
        }
    }
    
    public function showPage(){
        
        if($this->total>$this->size){
        
            $str ="<div id='page'>";
            $str.=$this->pageHome();
            $str.=$this->prePage();
            $str.=$this->midPage();
            $str.=$this->nextPage();
            $str.=$this->pageLast();
            
            $str.=$this->countTotal();
            
            $str.='<div>';
        }else{
            $str=null;
        }
        return $str;
        
    }
    
    private function pageHome(){
        if($this->cur==1){
            $str = '<p>首页</p>';
        }else{
            $str = '<a href=?page=1>首页</a>';
        }
        return $str;
    }
    
    private function prePage(){
        if($this->cur==1){
            $str = '<p>上一页</p>';
        }else{
            $str = '<a href=?page='.($this->cur-1).'>上一页</a>';
        }
        return $str;
    }
    
    private function midPage(){
        $str = '';
        if($this->pageIndex!=1){
            $str.='<p class="pageEllipsis">...</p>';
        }
        for($i=$this->pageIndex;$i<=$this->pageEnd;$i++){
            if($i==$this->cur){
                $str.="<a  class=cur  href=?page=$i>$i</a>";
            }else{
                $str.="<a   href=?page=$i>$i</a>";
            }
        }
        if($this->pageEnd!=$this->pageCount){
            $str.='<p class="pageEllipsis">...</p>';
        }
        return $str;
    }
    
    private function nextPage(){
        if($this->cur==$this->pageCount){
            $str = '<p>下一页</p>';
        }else{
            $str = '<a href=?page='.($this->cur+1).'>下一页</a>';
        }
        return $str;
    }
    
    private function pageLast(){
        if($this->cur==$this->pageCount){
            $str = '<p>尾页</p>';
        }else{
            $str = '<a href=?page='.$this->pageCount.'>尾页</a>';
        }
        return $str;
    }
    
    //统计
    private function countTotal(){
        return '<p class="pageRemark">共<b>'.$this->pageCount.'</b>页<b>'.$this->total.'</b>条数据</p>';
    }
    
}

分页类的使用以及展示

<?php
 require_once 'page.php';
 
 try{
    $db = new PDO('mysql:host=127.0.0.1;dbname=lampol;port=3306;charset=utf8','root','123456');
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
    echo 'connect Mysql Error:'.$e->getMessage();
}

$cur = $_GET['page'];
$size =2;

$sql = 'SELECT user_name,password FROM `user`';
$stmt = $db->prepare($sql);
$stmt->execute();
$total=$stmt->rowCount();//总的条数

// 0  8
// 8 ,8
//16, 8
$sql.='LIMIT '. ($cur-1)*$size.','.$size;

$stmt = $db->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll();

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>分页类</title>
</head>

<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>

<body>

<ul>
    <?php foreach($res as $row ): ?>
    <li><?php echo $row['user_name']  ?></li>
    <?php endforeach ?>
</ul>

<?php

//开始调用分页类
$page = new Page($cur,$total,$size);
echo $page->showPage();

?>


</body>
</html>

下面是效果图


QQ截图20180623181856.jpg

253 条留言此文章没有人评论

给我留言

评论内容