Home > Backend Development > PHP Problem > How to implement query paging to display page numbers in php

How to implement query paging to display page numbers in php

PHPz
Release: 2023-03-29 11:00:13
Original
1145 people have browsed it

In web development, data query and display are very common operations. For paging display of the queried data, the corresponding page number needs to be displayed on the page, which is a necessary requirement for users to facilitate page turning operations. This article will explain how to implement query paging through PHP code and display the corresponding page numbers in the list page.

1. Basic principles of query paging

A complete paging function needs to consider multiple factors such as query conditions, number of paging, and current page number. In the specific implementation, you need to first query the total number of all data that meets the conditions through the query conditions, then calculate the total number of pages based on the total number of data and the number displayed on each page, perform paging queries, and finally display the corresponding data and page numbers on the front end.

The specific process is as follows:

    <li>Get the query conditions <li>Query the total amount of data that meets the conditions <li>Calculate the total page based on the number displayed on each page Number <li>Get the current page number requested by the user <li>Calculate the starting position of the paging query <li>Perform the paging query operation <li>Display the data and the corresponding page on the front-end page Page

2. PHP implements query paging

Next, we implement the query paging function through PHP code.

2.1 Query the total amount

First, we need to query the total amount of data that meets the conditions. Suppose we have a data table named items, in which there is a status field indicating the data status. We need to query the total number of all data records with the status 1, The code is as follows:

// 连接数据库
$conn = mysqli_connect('<host>', '<username>', '<password>', '<database>');

// 构造查询SQL,获取符合条件的数据总数
$sql = "SELECT COUNT(*) AS total FROM `items` WHERE `status`=1";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($res);
$total = $row['total'];
Copy after login

In this code, we first connect to the database through the mysqli_connect function, then construct the query SQL, and perform the query operation with the help of the mysqli_query function, and finally Get the total number from the query results through the mysqli_fetch_assoc function.

2.2 Calculate the total number of pages and the current page number

Next, we need to calculate the total number of pages and the current page number requested by the user based on the number displayed on each page and the total amount of data.

// 定义每页显示的数据条数和当前请求的页码,默认为1和1
$limit = 10; // 每页显示的数据条数
$page = isset($_GET['page']) ? intval($_GET['page']) : 1; // 当前请求的页码

// 计算总页数
$totalPage = ceil($total / $limit);
if ($page > $totalPage || $page < 1) {
    $page = 1;
}

// 计算分页查询的起始位置
$offset = ($page - 1) * $limit;
Copy after login

Among them, we check whether there are page parameters in the request parameters through the isset function, and if so, convert them through the intval function Is an integer; if not, it defaults to the first page. Then the total number of pages is calculated through the ceil function, and the validity of the requested page number is judged.

Finally, we need to calculate the starting position of the paging query in order to perform paging queries in the database. We use a simple multiplication operation to calculate the offset at which the data starts to be queried.

2.3 Execute paging query

With the total data volume, total number of pages and query offset, we can perform paging query. The following is a code example for querying paging data:

// 查询数据(以status字段为1的数据为例)
$sql = "SELECT * FROM `items` WHERE `status`=1 LIMIT {$offset},{$limit}";
$res = mysqli_query($conn, $sql);
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
Copy after login

This code snippet passes the query offset and the number of displays per page into the LIMIT keyword in the SQL statement to perform the paging query operation. . The query results are obtained through the mysqli_fetch_all function and returned to the caller in the form of a PHP array.

2.4 Display page numbers in paging

Finally, we need to display the query results and corresponding page numbers on the front-end page. For page number display, we usually use HTML’s <ul> and <code><li> elements to wrap the page number link of each page in <li> element, and use <ul> elements to wrap all page number links.

<!-- 分页展示 -->
<ul>
    <?php for ($i = 1; $i <= $totalPage; $i++) : ?>
        <li <?php if ($i == $page) echo &#39;class="active"&#39; ?>>
            <?php if ($i == $page) : ?>
                <?php echo $i; ?>
            <?php else : ?>
                <a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
            <?php endif; ?>
        </li>
    <?php endfor; ?>
</ul>
Copy after login

In the above code, we use a for loop to traverse all page numbers. For the current page number, we add a ## to its corresponding <li> element. #active class to facilitate front-end style processing. At the same time, we use conditional statements to process the display of the current page number. The current page number does not need to add a link, it can be output directly through the echo statement; while other page numbers need to be output through element adds a link.

3. Summary

This article introduces how to implement query paging through PHP and how to display the paging page number in the front-end page. Implementing the paging function is a common task in web development. Through the introduction of this article, I believe that everyone has a deeper understanding of the paging function.

The above is the detailed content of How to implement query paging to display page numbers in php. For more information, please follow other related articles on the PHP Chinese website!

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