仿tp5.0分页 实现逻辑

/**
 * 分页
 */
public function getPage(){
    // 查询状态为1的用户数据 并且每页显示20条数据 总记录数为$count
    $count = model('mi_pay/Orderex')->_where($this->g_where)->num();
    
    $length = ceil($count/20); //数量
    $page = input('page',1); //页数
    
    $first_ellipsis = false; //第一个省略号
    $first_num = 3; //左边届
    $second_num = $length-2; //右边界
    $second_ellipsis = false; //第二个省略号
    
    $model = request()->module(); //模块
    $controller = request()->controller(); //控制器
    $function = request()->action(); //方法
    
    $li_a_str = '<li><a href="/'.$model.'/'.$controller.'/'.$function.'.html?page=';
    $li_ellipsis = '<li class="disabled"><span>...</span></li>'; //省略号
    
    if ($length>11) {
        if ($page > 6) {
            $first_ellipsis = true;
            if ($page + 5 < $length) {
                $first_num = $page - 3;
            } else {
                $first_num = $length - 8;
            }
        }
        if ($page < $length-5) {
            $second_ellipsis = true;
            if ($page >= 7) {
                if ($page + 5 < $length) {
                    $second_num = $page + 3;
                } else {
                    $second_num = $length -2;
                }
            } else {
                $second_num = 8;
            }
        }
    }
    
    $new_page = '<ul class="pagination">';
    
    //上一页
    $new_page .= $page != 1 ? $li_a_str.($page-1).'">«</a></li>' : '<li class="disabled"><span>«</span></li>';
    
    for ($i = 1; $i <= $length; $i++) {
        $tmp = ($i != $page) ? $li_a_str.$i.'">'.$i.'</a></li>' : '<li class="active"><span>'.$i.'</span></li>';

        //1,2
        if ($i < 3) {
            $new_page .= $tmp;
        }
        
        // 中间
        if($i >= $first_num && $i <= $second_num){
            //第一个省略号
            if ($i == $first_num && $first_ellipsis) {
                $new_page .= $li_ellipsis;
            }
            
            $new_page .= $tmp;
            
            //第二个省略号
            if ($i == $second_num && $second_ellipsis) {
                $new_page .= $li_ellipsis;
            }
        }
        
        //倒数1,2
        if ($i > 2 && $i > $length - 2) {
            $new_page .= $tmp;
        }
    }
    
    //下一页
    $new_page .= $page != $length ? $li_a_str.($page+1).'">»</a></li>' : '<li class="disabled"><span>»</span></li>';
    
    $new_page .= '</ul>';
    
    $this->assign('new_page', $new_page);
}

效果图

猜你喜欢

转载自blog.csdn.net/m0_37711659/article/details/84561620