为了账号安全,请及时绑定邮箱和手机立即绑定

php之分页类独立完成(未精简)

<?php
/************
*
*    subPage.class.php
*    分页类
*
*************/
class SubPage{
    private $sizeInPage;//每页显示多少条记录
    private $totalRecord;//总共多少条记录
    private $totalPage;//总共多少页
    private $currentPage;//当前页
    private $startIndex;//开始索引
    private $offset;//偏移数

    private $firstPage;//首页
    private $endPage;//尾页
    private $next;//下一页
    private $last;//上一页
    private $subPage_arr;//分页数组
    private $subPage_count;//分页数组显示的个数
    private $resultSet;//结果集


    /*
    *    @function 构造方法
    *    @param    $size_in_page每页显示记录数,$current_page当前页,$toatal_record总记录数,$subPage_count分页列数组显示的个数
    *    @return 
    */
    public function __construct($size_in_page,$current_page,$total_record,$subPage_count){
        //$this->initPageArr($page_count);
        $this->init($size_in_page,$current_page,$total_record,$subPage_count);
        $this->setTotalPage();
        $this->setStartIndex();
        $this->setOffset();
        if($this->is_SubPage()){
            //有分页的情况
            $this->setSubPageArr();
        }
        else{
            //没有分页的情况
            $this->subPage_arr=array();
        }
    }
    
    /*
    *    @function 初始化对象属性
    *    @param    $size_in_page每页显示记录数,$current_page当前页,$toatal_record总记录数
    *    @return    none
    */
    public function init($size_in_page,$current_page,$total_record,$subPage_count){
        $this->sizeInPage=$size_in_page;
        $this->currentPage=$current_page;
        $this->totalRecord=$total_record;
        $this->subPage_count=$subPage_count;
    }
    
    /*
    *    @function 设定总页数 
    */
    public function setTotalPage(){
        $this->totalPage=ceil($this->totalRecord/$this->sizeInPage);
    }
    
    /*
    *    @function 设定开始索引
    */
    public function setStartIndex(){
        $this->startIndex=($this->currentPage-1)*$this->sizeInPage;
    }
    
    /*
    *    @function 设定偏移量
    */
    public function setOffset(){
        $this->offset=$this->sizeInPage;
    }
    
    /*
    *    @function 设定首页的页数
    */
    public function setFirstPage(){
        $this->firstPage=1;
    }

    /*
    *    @function 设定尾页的页数
    */
    public function setEndPage(){
        $this->endPage=$this->totalPage;
    }
    
    /*
    *    @function 设定下一页的页数
    */
    public function setNext(){
        $tmp_next=$this->currentPage+1;
        if($tmp_next>$this->totalPage){
            $tmp_next=$this->totalPage;
        }
        $this->next=$tmp_next;
    }
    
    /*
    *    @function 设定上一页的页数
    */
    public function setLast(){
        $tmp_last=$this->currentPage-1;
        if($tmp_last<1){
            $tmp_last=1;
        }
        $this->last=$tmp_last;
    }

    /*
    *    @function 判断是否有分页
    *    @return boolean 返回true表示有分页,false表示没有
    */
    public function is_SubPage(){
        if($this->totalPage>1){
            return true;
        }
        else{
            return false;
        }
    }
    
    /*
    *    @function 组装分页数组
    *    @param $start开始的页码数,$count几个页码数
    */
    public function initSubPageArr($start,$count){
        $arr=array();
        for($i=0;$i<$count;$i++,$start++){
            $arr[$i]=$start;
        }
        $this->subPage_arr=$arr;
    }
    
    /*
    *    @function 检测数值是否奇数
    *    @param $num被检测数
    *    @return boolean 返回true表示是奇数,返回false表示是偶数
    */
    public function is_odd($num){
        if($num%2==0){
            return false;
        }
        else{
            return true;
        }
    }
    
    /*
    *    @function 设定分页数组
    *    @param none
    *    @return none
    */
    public function setSubPageArr(){
        if($this->totalPage<=$this->subPage_count){
            //只有一轮数组
            $start=1;
            $count=$this->totalPage-$start+1;
            $this->initSubPageArr($start,$count);
        }
        else{
            if($this->currentPage<ceil($this->subPage_count/2)){
                //第一轮数组 当前页小于均分数的情况
                $start=1;
                $count=$this->subPage_count-$start+1;
                $this->initSubPageArr($start,$count);
            }
            else if($this->currentPage<=$this->totalPage && $this->currentPage>($this->totalPage-ceil($this->subPage_count/2))){
                //最后一轮数组 当前页大于总页数-均分数
                $start=$this->totalPage-$this->subPage_count+1;
                $count=$this->subPage_count;
                $this->initSubPageArr($start,$count);
            }
            else{
                //其余情况
                if(($this->currentPage%$this->subPage_count)>ceil($this->subPage_count/2)){
                    //点到数组后半部分
                    $rs=$this->is_odd($this->subPage_count);
                    if($rs===true){
                        //显示数是奇数
                        $start=$this->currentPage-floor($this->subPage_count/2);
                        $count=$this->subPage_count;
                        $this->initSubPageArr($start,$count);
                    }
                    else{
                        //显示数是偶数
                        $start=$this->currentPage-(($this->subPage_count/2)-1);//因为当前页数也需要占1个位置
                        $count=$this->subPage_count;
                        $this->initSubPageArr($start,$count);
                    }
                }
                else{
                    //点到数组前半部分
                    $rs=$this->is_odd($this->subPage_count);
                    if($rs===true){
                        //显示数是奇数
                        $start=$this->currentPage-floor($this->subPage_count/2);
                        $count=$this->subPage_count;
                        $this->initSubPageArr($start,$count);
                    }
                    else{
                        //显示数是偶数
                        $start=$this->currentPage-(($this->subPage_count/2)-1);//因为当前页数也需要占1个位置
                        $count=$this->subPage_count;
                        $this->initSubPageArr($start,$count);
                    }
                }
            }
        }
    }
    
    /*
    *    @function 查看分页数的效果
    *    @param none
    *    @return string 分页码效果
    */
    public function showSubPageArr(){
        $arr=$this->subPage_arr;
        $str="";
        foreach($arr as $key=>$value){
            $str.=$value."_";
        }
        $str=rtrim($str,"_");
        echo $str;
    }    
}
$subPage=new SubPage(2,7,19,6);
$subPage->showSubPageArr();
?>

刚写的一个分页类,还未进行代码精简。各位大大们看看,哪里写的不对的,希望各位大大,前辈指出。谢谢!本人菜鸟一个虚心求教。

正在回答

0 回答

举报

0/150
提交
取消
PHP进阶篇
  • 参与学习       181826    人
  • 解答问题       2577    个

轻松学习PHP中级课程,进行全面了解,用PHP快速开发网站程序

进入课程

php之分页类独立完成(未精简)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信