分页的类

<?php
    class Page{
        var $total_nums        = 100 ; //信息总数
        var $page_nums        = 10;   //总页数
        var $current_page    = 1 ;    //当前页
        var $page_size        = 10;   //每页显示的信息条数
        var $param;                    //绑定参数
        var $url_link ;                //URL链接地址
        var $pagelen        = 5;    //每次显示的页码个数,如1 2 3 4 5   或者 6 7 8 9 10
       

        ##构造函数,初始化数据
        function __construct($total_nums,$page_nums,$current_page,$page_size,$param,$url_link,$pagelen){
            $this->total_nums    = intval($total_nums);
            $this->page_size    = intval($page_size)<1 ? 10 : intval($page_size);
            echo $page_nums;
            $this->page_nums    = intval($page_nums)>ceil($this->total_nums/$page_size) ? ceil(($this->total_nums/$page_size)) : intval($page_nums);
            $this->current_page    = intval($current_page) <1 ? 1 : intval($current_page);
            $this->current_page    = $this->current_page > $this->page_nums ? $this->page_nums : $this->current_page ;
            $this->pagelen        = intval($pagelen) <1 ? 5 : intval($pagelen);
            $this->param        = $param;
            $this->url_link        = $url_link;
            $this->countpage();
            $this->paramProcess();

        }

        ##分页算法
        function countpage(){
            $this->start    = 1;
            $this->end    = $this->page_nums;
            $page_len    = ($this->pagelen%2) ? $this->pagelen : $this->pagelen+1;//页码个数
            $pageoffset    = ($page_len-1)/2;//页码个数左右偏移量
            //分页数大于页码个数时可以偏移
            if($this->page_nums>$page_len){
                //如果当前页小于等于左偏移
                if($this->current_page<=$pageoffset){
                    $this->start    = 1;
                    $this->end    = $page_len;
                }else{
                    //如果当前页大于左偏移
                    //如果当前页码右偏移超出最大分页数
                    if($this->current_page+$pageoffset>$this->page_nums+1){
                        $this->start    = $this->page_nums-$page_len+1;
                    }else{
                        //左右偏移都存在时的计算
                        $this->start    = $this->current_page-$pageoffset;
                        $this->end    = $this->current_page+$pageoffset;
                    }
                }
            }
        }

        ##处理$param
        function paramProcess(){
            $url_link    = array();
            foreach($this->param as $k=>$v){
                if(!empty($v)){
                    $url_link[]    = $k.'='.urlencode($v);
                }
            }
            $link        = implode('&',$url_link);
            $url    = !empty($link)? '?'.$link.'&page=' : '?page=';
            return $url;
        }

        ##取得上一页和首页
        function getPre(){
            if($this->current_page != 1){
                $pagePre    .= "<a href=/"".$this->url_link.$this->paramProcess()."1/"> 首页 </a>";
                $pagePre    .= "<a href=/"".$this->url_link.$this->paramProcess().($this->current_page-1)."/"> 上一页 </a>";
            }else{
                $pagePre    .= "首页";
                $pagePre    .= "上一页";
            }
            return $pagePre;
        }
   
        ##取得下一页和尾页
        function getNext(){
            if($this->current_page != $this->page_nums){
                $pageNext    .= "<a href=/"".$this->url_link.$this->paramProcess().(($this->current_page)+1)."/"> 下一页  </a>";
                $pageNext    .= "<a href=/"".$this->url_link.$this->paramProcess().$this->page_nums."/">  尾页 </a>";
            }else{
                $pageNext    .= "下一页";
                $pageNext    .= "尾页";
            }
            return $pageNext;
        }

        ##输出分页形式
        function pageData(){
            $pageCode['pre']    = $this->getPre(); ##输出首页、上一页
            $pageCode['next']    = $this->getNext();
            for($i=$this->start;$i<=$this->end;$i++){
                if($i==$this->current_page){
                    $pageCode['len']    .= '<span>'.$i.'</span>';
                }else{
                    $pageCode['len']    .= "<a href=/"".$this->url_link.$this->paramProcess().$i."/"> ". $i ." </a>";;
                }
            }
           
            return $pageCode;
        }




    }
?>

用法

<?php
    include_once('test_page_class.php');
    $param    = array('name'=>'名字','title'=>'标题','content'=>'');
    $total_nums    = 10000;
    $page_size    = 10;
    $page_nums    = ceil($total_nums/$page_size);
    $current_page    = intval($_GET['page']) < 1 ? 1 : intval($_GET['page']);
    $current_page    = $current_page>$page_nums ?  $page_nums : $current_page;
    $url_link        = 'test_page.php';
    $pagelen        = 10;
    $pg    = new Page($total_nums,$page_nums,$current_page,$page_size,$param,$url_link,$pagelen);
    $page_data    = $pg->pageData();
    echo $page_data['len'];
    echo $page_data['pre'];
    echo $page_data['next'];
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值