TP5分页使用方法说明

TP5分页使用方法说明
TP5分页的使用方法,在手册中有详细的说明

控制器中关键代码如下:

// 查询状态为1的用户数据 并且每页显示10条数据 l i s t = D b : : n a m e ( u s e r ) > w h e r e ( s t a t u s , 1 ) > p a g i n a t e ( 10 ) ; / / l i s t list = Db::name('user')->where('status',1)->paginate(10);// 把分页数据赋值给模板变量list this->assign(‘list’, l i s t ) ; / / r e t u r n list);// 渲染模板输出return this->fetch();

模板文件中分页输出代码如下:

{volist name=‘list’ id=‘user’}

{$user.nickname}

{/volist}

{$list->render()}

上面的方法非常简单,但是如果我想在查询出来的数据中加入新的值的,上面的方法就不能用了,当你尝试对$list进行循环的时候,会报如下的错误

Indirect modification of overloaded element of think\paginator\Collection has no effect

这是因为$list不是一个数组,而是数据集对象think\Collection手册地址

扫描二维码关注公众号,回复: 3968052 查看本文章

select()返回的是二维数组结果集,paginate()返回的是对象类型的结果集

下面是我的处理方法

// 查询状态为1的用户数据 并且每页显示10条数据 l i s t = D b : : n a m e ( u s e r ) > w h e r e ( s t a t u s , 1 ) > p a g i n a t e ( 10 ) ; / / list = Db::name('user')->where('status',1)->paginate(10);// 获取分页显示 page = l i s t > r e n d e r ( ) ; list->render(); data = l i s t > a l l ( ) ; f o r e a c h ( list->all();foreach( dataas k e y = > key=> val){ d a t a [ data[ key][‘key’] = KaTeX parse error: Expected 'EOF', got '}' at position 5: key;}̲this->assign(‘data’, d a t a ) ; data); this->assign(‘page’, p a g e ) ; / / r e t u r n page);// 渲染模板输出return this->fetch();

模板文件中分页输出代码如下:

{volist name=‘data’ id=‘user’}

{$user.nickname}

{/volist}

{$page}

猜你喜欢

转载自blog.csdn.net/qq_43044679/article/details/83104943