Detailed explanation of PHP paging principles and implementation methods, allowing you to easily master it

PHPz
Release: 2024-02-29 15:20:01
Original
580 people have browsed it

Detailed explanation of PHP paging principles and implementation methods, allowing you to easily master it

PHP paging is a common function in web development, especially when displaying a large amount of data. In order to avoid interface confusion and improve user experience, the paging function is particularly important. This article will explain in detail the principles and implementation methods of PHP paging, and provide specific code examples to help readers easily master this technology.

1. Paging principle

The principle of paging is to divide the data in the database into multiple pages for display according to the user's needs. The user can switch the displayed data by clicking on different page numbers. Generally speaking, the paging function requires two key parameters: the current page number and the number of data items displayed on each page. Calculate the starting position and ending position of the data to be displayed in the database through a reasonable algorithm to achieve the paging effect.

2. Implementation method

Step 1: Connect to the database

First, we need to connect to the database and obtain the total number of data and the data displayed on each page. Here is the MySQL database as an example:

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 获取数据总条数
$sql = "SELECT COUNT(*) as total FROM table_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_rows = $row['total'];

// 每页显示的数据条数
$per_page = 10;
Copy after login

Step 2: Calculate paging information

Next, based on the current page number and the number of data displayed on each page, calculate the data that needs to be displayed in the database The starting position and ending position:

if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 1;
}

$start = ($page - 1) * $per_page;
Copy after login

Step 3: Query the data and display

According to the calculated starting position and ending position, query the data in the database and display it:

$sql = "SELECT * FROM table_name LIMIT $start, $per_page";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
    }
} else {
    echo "0 结果";
}
Copy after login

Step 4: Display paging links

The last step is to display paging links so that users can easily switch between different page numbers:

$total_pages = ceil($total_rows / $per_page);

for ($i = 1; $i <= $total_pages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
Copy after login

3. Complete code example

Integrate the above codes to realize a complete PHP paging function:

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT COUNT(*) as total FROM table_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_rows = $row['total'];

$per_page = 10;

if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 1;
}

$start = ($page - 1) * $per_page;

$sql = "SELECT * FROM table_name LIMIT $start, $per_page";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
    }
} else {
    echo "0 结果";
}

$total_pages = ceil($total_rows / $per_page);

for ($i = 1; $i <= $total_pages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
Copy after login

Through the above detailed steps and code examples, I believe that readers have mastered the principles and implementation methods of PHP paging. The paging function can make web pages more friendly and easier to use. I hope this article will be helpful to you.

The above is the detailed content of Detailed explanation of PHP paging principles and implementation methods, allowing you to easily master it. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!