美文网首页
php写的分页类

php写的分页类

作者: limgquan | 来源:发表于2017-03-14 23:21 被阅读0次
<?php
class Page {
    public $totalRows;                      //总记录数
    public $pageSize;                       //每页几条
    public $limit;                          //limit字串
    public $currentPageNum;                 //当前第几页
    public $totalPageNum;                   //总页数
    private $url;                           //当前url
    private $bothNum = 4;                   //分页数字当前页两边有几个可点页
    private $sign;                          //url中page前面是&还是?

    //构造方法
    public function __construct($totalRows, $pageSize = 2){
        $this->totalRows = $totalRows ? $totalRows : 1;
        $this->pageSize = $pageSize;
        $this->totalPageNum = ceil($this->totalRows / $this->pageSize);
        $this->currentPageNum = $this->getCurrentPage();
        $this->url = $this->getUrl();
        $this->limit = $this->getLimit();
    }



    public function show(){
        echo $this->first();
        echo $this->prev();
        echo $this->pageList();
        echo $this->next();
        echo $this->last();
        // echo '<hr>';
        // echo $page->limit;
    }

    /*
        1 当前页不存在 1
        2 当前页存在 但大于总页数 取总页数
        3 当前页存在 在1~总页数之间 去当前页

    */
    private function getCurrentPage(){
         if (!empty($_GET['page'])) {  
                if ($_GET['page'] > 0) {
                   if ($_GET['page'] > $this->totalPageNum) {  
                          return $this->totalPageNum;  
                   } else {  
                          return $_GET['page'];  
                   }  
                } else {  
                   return 1;  
                }  
         } else {  
                return 1;  
         }  
    }

    //获取当前url并解析参数去掉page参数 确定page前是? or &
    private function getUrl(){
        // echo '<pre>';
        // print_r($_SERVER);
        // echo '</pre>';exit;

        $url = $_SERVER['REQUEST_URI'];
        $urlDetail = parse_url($url);
        // echo '<pre>';
        // print_r($urlDetail);
        // echo '</pre>';exit;
        if(isset($urlDetail['query'])){
            parse_str($urlDetail['query'],$params);
            unset($params['page']);
            if(count($params) > 0) {
                $url = $urlDetail['path'].'?'.http_build_query($params);
                $this->sign = '&';
            } else {
                $url = $urlDetail['path'];
                $this->sign = '?';
            }
        }
        return $url;
    }

    private function getLimit(){
        if ($this->currentPageNum == $this->totalPageNum) {
            return 'LIMIT '.($this->currentPageNum - 1) * $this->pageSize . ', '.($this->totalRows - ($this->currentPageNum - 1) * $this->pageSize);
        } else {
            return 'LIMIT '.($this->currentPageNum - 1) * $this->pageSize . ', '. $this->pageSize;
        }
    }

    /*
        页码表 默认当前页前后各4个可点的页面
        首页 上一页... 1234 5 6789 ...下一页 尾页
    */
    private function pageList(){
        $pageList = '';

        if ($this->currentPageNum - $this->bothNum > 1) {
            $pageList .= ' ... ';
        }
        //当前页前边的部分
        for($i = $this->bothNum;$i>0;$i--) {
            $_page = $this->currentPageNum - $i;
            if ($_page < 1 ) continue;
            $pageList .= ' <a href="'.$this->url.$this->sign.'page='.$_page.'">'.$_page.'</a> ';

        }
        //当前页
        $pageList .= ' ' . $this->currentPageNum . ' ';
        //当前页之后的部分
        for ($i=1;$i<=$this->bothNum;$i++){
            $_page = $this->currentPageNum + $i;
            if ($_page > $this->totalPageNum) continue;
            $pageList .=' <a href="'.$this->url.$this->sign.'page='.$_page.'">'.$_page.'</a> ';
        }

        if ($this->currentPageNum + $this->bothNum < $this->totalPageNum) {
            $pageList .= ' ... ';
        }
        return $pageList;

    }

    //首页
    private function first(){
        if ($this->currentPageNum > 1) {
            return '<a href="'.$this->url.$this->sign.'page=1">首页</a>';
        }
    }

    //尾页
    private function last(){
        if ($this->currentPageNum < $this->totalPageNum) {
            return '<a href="'.$this->url.$this->sign.'page='.$this->totalPageNum.'">尾页</a>';
        }
    }

    //上一页
    private function prev(){
        if($this->currentPageNum > 1) {
            return ' <a href="'.$this->url.$this->sign.'page='.($this->currentPageNum - 1).'">上一页</a> ';
        }
    }

    //下一页
    private function next(){
        if($this->currentPageNum < $this->totalPageNum) {
            return ' <a href="'.$this->url.$this->sign.'page='.($this->currentPageNum + 1).'">下一页</a> ';
        }
    }

} 

使用该分页类:

<?php
include 'Page.php';
$link =mysqli_connect('127.0.0.1','root','','test');
$sql = "select count(*) as totalRows from user";
$res = mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($res)) {
    // var_dump($row);
    $totalRows = $row['totalRows'];
    // var_dump($totalRows);
}
// echo mysqli_error($link);
// var_dump($res);exit;
$pageSize = 3;
$page = new Page($totalRows,$pageSize);

$sql = "select * from user ".$page->limit;
$res =mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($res)) {
    echo '-----'.$row['id'].'-----'.$row['name'].'-----<br>';
}
// echo $page->getUrl();
// echo $page->first();
// echo $page->prev();
// echo $page->pageList();
// echo $page->next();
// echo $page->last();
// echo '<hr>';
// echo $page->limit;

// insert into user (name) values ('茹雪'),('正梅'),('美琳'),('欢馨'),('优璇'),('雨嘉'),('娅楠'),('明美'),('可馨'),('惠茜'),('漫妮'),('香茹'),('月婵'),('嫦曦'),('静香'),('梦洁'),('凌薇'),('美莲'),('雅静'),('雪丽'),('依娜'),('雅芙'),('雨婷'),('语嫣'),('桑榆'),('曼玉'),('歆瑶'),('凌菲'),('靖瑶'),('瑾萱'),('佑怡'),('婳祎'),('檀雅'),('若翾'),('熙雯'),('诗茵'),('静璇'),('婕珍'),('沐卉'),('琪涵'),('佳琦'),('昭雪'),('倩雪'),('梦琪'),('忆柳'),('之桃'),('慕青'),('问兰'),('尔岚'),('元香'),('初夏'),('沛菡'),('傲珊'),('曼文'),('乐菱'),('痴珊'),('惜文'),('香寒'),('新柔'),('语蓉'),('海安'),('夜蓉'),('涵柏'),('水桃'),('语琴');
$page->show();

相关文章

网友评论

      本文标题:php写的分页类

      本文链接:https://www.haomeiwen.com/subject/ckuknttx.html