Home >PHP Framework >ThinkPHP >How to do paging in thinkphp
1. Code implementation in the controller
In the controller method, we can use the built-in paging class \think\paginator\driver of the TP framework \Bootstrap to complete the implementation of data paging function. We can first query the data to be paging, then pass the query results to the paging class, and then call the render() method of the paging class.
The following is an example of controller code:
use \think\paginator\driver\Bootstrap;
public function index()
{
// 查询文章列表数据 $articles = Db::name('article')->paginate(10); // 将查询结果传递给分页类 $page = $articles->render(); // 将分页后的数据传递给模板 $this->assign('articles', $articles); $this->assign('page', $page); return $this->fetch('index');
}
The amount of data displayed per page is specified as 10, which is set through the parameters in the paginate() method in the sample code. The $articles variable stores the queried article list data, and the $page variable stores the paging HTML code.
2. Code implementation in the template
In the template, we can return the paging HTML code through the render() method of the paging class, and then render the paging on the page navigation.
The following is an example of template code:
ff6d136ddc5fdfeffaf53ff6ee95f185
{volist name="articles" id="article"} <li>{$article.title}</li> {/volist}
929d1f5ca49e04fdcb27f9465b944689
a1fb01281cee74847a3dc20ff046025f
{$page}
16b28748ea4df4d9c2150843fecfba68
We used the volist tag of the TP framework to loop out the article list in the code. We use {$page} to output the HTML code for paging navigation after the loop ends.
The above is the detailed content of How to do paging in thinkphp. For more information, please follow other related articles on the PHP Chinese website!