thinkphp6:原生Db::query()查询分页实现

...//省略代码
//引入框架自带分页渲染类
use think\paginator\driver\Bootstrap;
class Atest extends BaseController
{
    //报名审核过渡页
    function articleList()
    {
        $pageNo = Request::param('page');
        //客户端传过来的分页
        $pageNumber = $pageNo ? $pageNo : '0';
        if($pageNumber > 0){
            $pageNumber_one = $pageNumber-1;
        } else {
            $pageNumber_one = 0;
        }
        $limit = 10;//每页显示条数
        $offset = $pageNumber_one * $limit;//查询偏移值
        $sql = "SELECT ...复杂的SQL脚本... limit $offset,$limit";
        $list = Db::query($sql);
        //查询的总条数
        $sqlTotal = "SELECT count(*) as count_num ...复杂的SQL脚本...";
        $counts = Db::query($sqlTotal);
        $count = count($counts);//因为获取的总数为数组类型,因此用count计算出总数
        //组合分页数据格式
        $pagernator = Bootstrap::make($list,$limit,$pageNumber,$count,false,['path'=>Bootstrap::getCurrentPath(),'query'=>request()->param()]);
        $page = $pagernator->render();
        // 获取分页显示
        View::assign(['list' => $list, 'page' => $page]);
        return View::fetch();
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注