纵有疾风起
人生不言弃

自定义简单分页类

<?php
    class Pages{        
        private $count;            //要显示的总条数
        private $page_num;        //每页显示的条数
        private $total_pages;     //总页数
        private $current;        //当前页
        private $offset;        //页码显示偏移量

        private $config=array(
            ‘previous’=>'<‘,    //上一页
            ‘next’=>’>’,        //下一页
            ‘start’=>'<<‘,        //首页
            ‘end’=>’>>’            //末页
        );

       //构造方法传入需要总条数和每页显示的条数  

        function __construct($count,$num,$offset=3){
            $this->count=$count;
            $this->page_num=$num;
            $this->current=isset($_GET[‘p’])?$_GET[‘p’]:1;
            $this->offset=$offset;
        }    

        //设置配置选项
        function setConfig($key,$value){

            foreach($this->config as $k=>$v){
                //遍历判断如果当前的配置项确实存在才可以进行重新设置
                if($k==$key){
                    $this->config[$key]=$value;                
                }    
            }
        }

        function show(){
            //计算出总页码数
            $this->total_pages=ceil($this->count/$this->page_num);

            //计算出上一页
            $previous=$this->current-1>0?$this->current-1:1;
            $previous=”<a href=’?p=$previous’>”.$this->config[‘previous’].”</a>&nbsp;”;
            
            //计算出下一页
            $next=$this->current+1>$this->total_pages?$this->total_pages:$this->current+1;
            $next=”<a href=’?p=$next’>”.$this->config[‘next’].”</a>&nbsp;”;

            //首页
            $first=”<a href=’?p=1′>”.$this->config[‘start’].”</a>&nbsp;”;
            //尾页
            $end=”<a href=’?p=$this->total_pages’>”.$this->config[‘end’].”</a>&nbsp;”;
            $linkpage=”;

            //最左位开始显示的页码数
            $leftpage=max($this->current-$this->offset,1);
            
            //最右位结束显示的页码数
            $rightpage=min($this->current+$this->offset,$this->total_pages);
            for($i=$leftpage;$i<=$rightpage;$i++){
                if($i==$this->current){
                    //给当前页添加样式去掉下划线
                    $linkpage.=”<a style=’text-decoration: none;’ href=’?p=$i’><font color=red>”.$i.”</font></a>&nbsp;”;
                }else{
                    $linkpage.=”<a href=’?p=$i’><font color=blue>”.$i.”</font></a>&nbsp;”;
                }
            }
                return $first.$previous.$linkpage.$next.$end;
        }
    }

    $page=new Pages(100,3);
    //设置配置
    $page->setConfig(‘previous’,’上一页’);
    $page->setConfig(‘next’,’下一页’);
    $page->setConfig(‘start’,’首页’);
    $page->setConfig(‘end’,’尾页’);
    $linkpage=$page->show();
    echo $linkpage;
?>

原文链接:https://blog.csdn.net/living_ren/article/details/78877315

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » 自定义简单分页类
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录