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.
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:
Next, we implement the query paging function through PHP code.
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'];
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.
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;
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.
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);
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.
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 'class="active"' ?>> <?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>
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.
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!