Home Backend Development PHP Problem How to perform page jump in ThinkPHP

How to perform page jump in ThinkPHP

Apr 13, 2023 am 09:05 AM

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:

  1. Instancing the paging class
$p = new \think\Paginator(100, 10);
Copy after login

The first parameter is the total number of records, and the second parameter Is the number of records displayed per page.

  1. Get paging data
$list = $p->getPageData();
Copy after login

The getPageData method returns the data of the specified page number.

  1. Rendering paging links
echo $p->render();
Copy after login

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

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.

  1. Modify the paging class

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; // 添加跳转目标页码参数
}
Copy after login
  1. Get the target page number

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

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!

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24