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:
{volist name="articles" id="article"} <li>{$article.title}</li> {/volist}