Home  >  Article  >  PHP Framework  >  How to set current page pagination in ThinkPHP 3.2

How to set current page pagination in ThinkPHP 3.2

PHPz
PHPzOriginal
2023-04-07 09:25:10580browse

ThinkPHP 3.2 is a powerful open source PHP framework. During the development process, the paging function is often used. This article will focus on how to set current page pagination in ThinkPHP 3.2.

First, you need to define the amount of data displayed on each page in the controller (the default is 20), and get the current page number. You can get the current page number through I('p',1). If no parameters are passed, it defaults to page 1.

public function getList(){
    $page_size = 10; // 每页显示10条数据
    $p = I('p',1); // 获取当前页码
    //TODO: ...
}

Then, you need to calculate the starting position of each page of data in order to perform paging queries in the database. Assume that the data in the query table user can be paged using the limit method provided by ThinkPHP.

public function getList(){
    $page_size = 10; // 每页显示10条数据
    $p = I('p',1); // 获取当前页码
    $user = M('User');
    $offset = ($p-1)*$page_size; // 计算当前页数据的起始位置
    $list = $user->limit($offset.','.$page_size)->select();// 按分页查找数据
}

In the above code, use $offset = ($p-1)*$page_size; when calculating the starting position of the data, and then use limit() Method to perform paging query.

Finally, the paging code needs to be rendered in the view file so that users can easily turn pages. You can use the page() method provided by ThinkPHP to generate Bootstrap-style paging code.

public function getList(){
    $page_size = 10; // 每页显示10条数据
    $p = I('p',1); // 获取当前页码
    $user = M('User');
    $offset = ($p-1)*$page_size; // 计算当前页数据的起始位置
    $list = $user->limit($offset.','.$page_size)->select();// 按分页查找数据
    $count = $user->count(); // 获取数据总数
    $Page = new \Think\Page($count,$page_size); // 实例化分页类
    $show = $Page->show(); // 分页显示输出
    $this->assign('list',$list);
    $this->assign('page',$show); // 分配分页代码到视图
    $this->display();
}

In the above code, use $count = $user->count(); to get the total number of data, and then use new \Think\Page($count,$page_size ); Instantiate the paging class, pass in the total number and the amount of data per page, and finally use $Page->show(); to generate the paging display string. Then pass the paging string to the view file and use $this->assign('page',$show); to assign the variable.

Summary

Setting the current page paging in ThinkPHP 3.2 requires the following three steps:

  1. Define the amount of data displayed on each page in the controller, and obtain the current Page number;
  2. Calculate the starting position of each page of data, and use the limit method for paging query;
  3. Generate paging strings in the view file and output them to facilitate users Page turning operation.

The above is the entire content of this article, I hope it will be helpful to you. If you have any questions or suggestions, please leave a message in the comment area and I will respond promptly.

The above is the detailed content of How to set current page pagination in ThinkPHP 3.2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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