search
HomePHP FrameworkThinkPHPHow to set current page pagination in ThinkPHP 3.2

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools