Paging principle and implementation


Paging implementation

ThinkPHP has built-in paging implementation. It is very simple to add paging output function to the data. It can be called directly during Db class query paginate method:

// 查询状态为1的用户数据 并且每页显示10条数据
$list = Db::name('user')->where('status',1)->paginate(10);
// 把分页数据赋值给模板变量list
View::assign('list', $list);
// 渲染模板输出
return View::fetch();

can also be changed to the model's paging query code:

// 查询状态为1的用户数据 并且每页显示10条数据
$list = User::where('status',1)->paginate(10);
// 把分页数据赋值给模板变量list
View::assign('list', $list);
// 渲染模板输出
return View::fetch();

The paging output code in the template file is as follows:

<div>
<ul>
{volist name='list' id='user'}
    <li> {$user.nickname}</li>
{/volist}
</ul>
</div>
{$list|raw}

You can also assign a separate value to the paging output template The paging output code in the variable

// 查询状态为1的用户数据 并且每页显示10条数据
$list = User::where('status',1)->paginate(10);
// 获取分页显示
$page = $list->render();
// 模板变量赋值
View::assign('list', $list);
View::assign('page', $page);
// 渲染模板输出
return View::fetch();

template file is as follows:

<div>
<ul>
{volist name='list' id='user'}
    <li> {$user.nickname}</li>
{/volist}
</ul>
</div>
{$page|raw}

By default, the generated paging output is a complete paging function, with total paging data and upper and lower page numbers. The paging style only needs to pass the style Just modify it. The default paging output code generated by complete paging is:

<ul class="pagination">
<li><a href="?page=1">&laquo;</a></li>
<li><a href="?page=1">1</a></li>
<li class="active"><span>2</span></li>
<li class="disabled"><span>&raquo;</span></li>
</ul>

If you need to obtain the total data separately, you can use

// 查询状态为1的用户数据 并且每页显示10条数据
$list = User::where('status',1)->paginate(10);
// 获取总记录数
$count = $list->total();
// 把分页数据赋值给模板变量list
View::assign('list', $list);
// 渲染模板输出
return View::fetch();

to pass in the total number of records

Supports passing in the total number of records without automatically calculating the total, for example:

// 查询状态为1的用户数据 并且每页显示10条数据 总记录数为1000
$list = User::where('status',1)->paginate(10,1000);
// 获取分页显示
$page = $list->render();
// 模板变量赋值
View::assign('list', $list);
View::assign('page', $page);
// 渲染模板输出
return View::fetch();

For UNION queries and some special complex queries, it is recommended to use this method to first query the total number of records separately. , and then pass in the paging method

Data processing after paging

Supports direct each traversal processing of data after paging class, which is convenient for modifying the data after paging, and Fields cannot only be supplemented through model getters.

$list = User::where('status',1)->paginate()->each(function($item, $key){
    $item->nickname = 'think';
});

If the Db class operates paging data, the return value needs to be used in the closure function of each method, for example:

$list = Db::name('user')->where('status',1)->paginate()->each(function($item, $key){
    $item['nickname'] = 'think';
    return $item;
});

Simple paging

If you only need to output a paging output with only upper and lower pages, you can use the following concise paging code:

// 查询状态为1的用户数据 并且每页显示10条数据
$list = User::where('status',1)->paginate(10, true);
// 把分页数据赋值给模板变量list
View::assign('list', $list);
// 渲染模板输出
return View::fetch();

The output code of concise paging mode is:

<ul class="pager">
<li><a href="?page=1">&laquo;</a></li>
<li class="disabled"><span>&raquo;</span></li>
</ul>

Due to simplicity Paging mode does not require the total number of data to be queried, so it can improve query performance.

Paging parameters

The main paging parameters are as follows:


## ParameterDescriptionlist_rowsQuantity per pagepageCurrent pagepathurl pathqueryurl extra parameters fragmenturl anchorvar_pagePage variable


The setting of paging parameters can be passed in when calling the paging method, for example:

$list = Db::name('user')->where('status',1)->paginate([
    'list_rows'=> 20,
    'var_page' => 'page',
]);

If you need to pass in the query conditions when paging , you can use the query parameter to splice additional query parameters

Custom paging class

If you need to customize paging, you can extend a paging driver.

Then rebind in the provider.php definition file

return [
    'think\Paginator'    =>    'app\common\Bootstrap'
];