Paging query


ThinkPHP has a built-in paging implementation. It is very simple to add paging output function to the data. You can directly call the paginate method when querying the Db class:

// 查询状态为1的用户数据 并且每页显示10条数据
$list = Db::name('user')->where('status',1)->order('id', 'desc')->paginate(10);

// 渲染模板输出
return view('index', ['list' => $list]);

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}

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 the template variable for paging output separately

// 查询状态为1的用户数据 并且每页显示10条数据
$list = Db::name('user')->where('status',1)->order('id', 'desc')->paginate(10);

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

return view('index', ['list' => $list, 'page' => $page]);

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>
{$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 be modified through the style. 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 If you need to obtain the total data separately, you can use

// 查询状态为1的用户数据 并且每页显示10条数据
$list = Db::name('user')->where('status',1)->order('id' ,'desc')->paginate(10);
// 获取总记录数
$count = $list->total();
return view('index', ['list' => $list, 'count' => $count]);

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 = Db::name('user')->where('status',1)->paginate(10,1000);
// 获取分页显示
$page = $list->render();

return view('index', ['list' => $list, 'page' => $page]);

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 it into the paging method

Data processing after paging

Supports direct each traversal processing of data after paging class, making it convenient to modify the data after paging, instead of only adding fields through the model's getter.

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

If the model class operates paging data, there is no need to use the return value in the closure function of each method, for example:

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

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 = Db::name('user')->where('status',1)->order('id', 'desc')->paginate(10, true);

// 渲染模板输出
return view('index', ['list' => $list]);

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>

Because Concise 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:

ParameterDescription
list_rowsNumber per page
pageCurrent page
pathurl path
queryurl extra parameters
fragmenturl anchor
var_pagePaging 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 query conditions when paging, you can use the query parameter to splice additional query parameters

Big Data Pagination

For paging queries of large amounts of data, the system provides a high-performance paginateX paging query method. The usage is somewhat different from paginate paging query. If the amount of data you want to query by page is more than one million, using the paginateX method will significantly improve it, especially when the number of pages is large. And because it is designed for large data volumes, this paging query can only use simple paging mode, so there is no total number.

The sorting field of paging query must use an index field and be a continuous integer, otherwise data will be missed.

The main scenario is to perform paging query based on the primary key. By default, the paging data is queried in reverse order of the primary key.

$list = Db::name('user')->where('status',1)->paginateX(20);

You can also specify the primary key and sorting when querying

$list = Db::name('user')->where('status',1)->paginateX(20, 'id', 'desc');

The query method will execute two queries. The first query is used to find the maximum or minimum value that satisfies the current query conditions. Then use the primary key query conditions to perform paging data query.

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'
];