thinkphp5.0分页

 第一种

public function index(){
        // 页面和面包屑导航
        $ttl[0] = $this->title;
        $ttl[1] = '管理员列表';
        $this->assign('ttl',$ttl);
        // 权限验证
        $this->admin_priv('role_index');

        $where = [];
     // 查询条件
        $keyword = input('param.keyword');
        if($keyword){
            $where['name'] = ['like','%'.$keyword.'%'];
        }
        // 查询
        $list = db("role")
            ->where($where)
            ->paginate(config('paginate.list_rows'));

        // 获取分页显示
        $page = $list->render();

        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->fetch();
    }

 第二种写法:

public function index(){
        // 页面和面包屑导航
        $ttl[0] = $this->title;
        $ttl[1] = '管理员列表';
        $this->assign('ttl',$ttl);
        // 权限验证
        $this->admin_priv('role_index');


        // 查询条件
        $keyword = input('param.keyword');
        $where['name'] = ['like','%'.$keyword.'%'];
        $fiels['keyword'] = $keyword;
        // 查询
        $list = db("role")
            ->where($where)
            ->paginate(config('paginate.list_rows'));

        // 获取分页显示
        $page = $list->render();

        // 模板变量赋值
        $this->assign('fiels', $fiels);

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->fetch();
    }

这两种只有细节方面的差别,其他都一样

注意:

1.$where 的初始条件为 $where = []  

$where = 1   报错:Illegal string offset 'name'

2.查询数组两种写法,都可以

$where['name'] = ['like','%'.$keyword.'%'];

$where['name']=array('like','%'.$keyword.'%');

原文地址:https://www.cnblogs.com/wesky/p/6245723.html