Home > Article > Backend Development > How to perform page jump in ThinkPHP
ThinkPHP is an excellent PHP development framework. It provides many convenient and fast methods for development, including paging. In web development, paging is one of the very important functions, which is used to divide a large amount of data into several pages for display. But sometimes, we need to jump to a specified page during paging. This article will introduce how to perform paging jump in ThinkPHP.
1. Basic use of ThinkPHP paging
In ThinkPHP, using paging is very simple, just use the paging class. The paging class provides the following basic usage methods:
$p = new \think\Paginator(100, 10);
The first parameter is the total number of records, and the second parameter Is the number of records displayed per page.
$list = $p->getPageData();
The getPageData method returns the data of the specified page number.
echo $p->render();
The render method returns an HTML string used to render paging links.
The above is the basic method of using ThinkPHP paging, which is very convenient and fast. But sometimes, we need to implement the paging jump function. The specific implementation method will be introduced below.
2. ThinkPHP paging jump
Before implementing paging jump, we need to understand the format of ThinkPHP paging link. Assuming we use the default paging style, the rendered link format is as follows:
http://localhost/index.php/index/index?page=2
Among them, the page parameter represents the current page number. Now, we need to add a jump target page number parameter to the paging link to implement the paging jump function.
In ThinkPHP's paging class Paginator, there is a makeUrl method for generating paging links. We can add a target page number parameter in this method, as shown below:
protected function makeUrl($page) { $url = str_replace('[PAGE]', $page, $this->urlTpl); return $url . '&target=' . $this->targetPage; // 添加跳转目标页码参数 }
Get the target page number in the controller and assign it a value Give the targetPage attribute of the paging class. The target page number can be obtained from GET, POST or other methods. Here we take the GET method as an example:
$page = input('page/d', 1); // 获取当前页码 $targetPage = input('target/d', $page); // 获取目标页码,默认为当前页码 $p = new \think\Paginator(100, 10); $p->targetPage = $targetPage; // 将目标页码赋值给分页类 $list = $p->getPageData(); echo $p->render();
In the above code, use the input function to obtain the GET parameters, and d means converting the parameters into integers. $page represents the current page number, $targetPage represents the target page number, and the default is the current page number. Assign the target page number to the targetPage attribute of the paging class. The paging class will use this attribute value as the jump target page number parameter when rendering the link.
At this point, we have completed the function of paging jump in ThinkPHP. When the user clicks on the paging link, the target page number parameter will be taken to the link on the next page, thus realizing the paging jump function.
3. Summary
ThinkPHP provides a convenient and fast paging class, which is very simple to use, but in some scenarios it is necessary to implement the paging jump function. This article introduces the specific method of implementing paging jumps in ThinkPHP. I hope it will be helpful to everyone.
The above is the detailed content of How to perform page jump in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!