Home > PHP Framework > ThinkPHP > How to do paging in thinkphp

How to do paging in thinkphp

王林
Release: 2023-06-01 09:13:22
forward
1138 people have browsed it

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');
Copy after login

}

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}
    Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
thinkphp versions supported by php6
From 1970-01-01 08:00:00
0
0
0
thinkphp upload files
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template