Home  >  Article  >  Backend Development  >  How to implement paging in php

How to implement paging in php

PHPz
PHPzOriginal
2023-04-23 10:21:561204browse

PHP is a popular server-side programming language that is widely used in the field of web development. Pagination is a very common requirement in website development. It can display a large amount of data in pages according to certain rules, making it convenient for users to browse. This article will introduce how to implement paging function in PHP.

  1. Get database data

In PHP, you can use extension libraries such as mysqli or PDO to connect to the database. We first need to get all the data that needs to be paginated from the database. Through SQL statements, we can use the LIMIT clause to obtain a specified amount of data. For example:

$per_page = 10; //每页显示的数据量
$page_num = $_GET['page'] ?? 1; //当前页码,默认为1
$start = ($page_num - 1) * $per_page; //起始位置

$sql = "SELECT * FROM products LIMIT $start, $per_page";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

In the above code, we use the mysqli extension library to connect to the MySQL database, and use the LIMIT clause to obtain the specified amount of data. Among them, $per_page represents the number of data displayed on each page, $page_num represents the current page number, and $start represents the starting position of data query. By calculating the starting position and the amount of data per page, we can construct a SQL statement to obtain data from the database.

  1. Display paging navigation bar

After obtaining the data, we need to display the paging navigation bar on the page to facilitate the user to turn pages. The following is a sample code:

$total_count = $conn->query("SELECT COUNT(*) FROM products")->fetch_row()[0]; //总记录数
$total_page = ceil($total_count / $per_page); //总页数
$start_page = max(1, $page_num - 5); //起始页码
$end_page = min($total_page, $start_page + 9); //结束页码

echo "<div class=&#39;pagination&#39;>";
if($page_num > 1) {
    echo "<a href=&#39;?page=".($page_num-1)."&#39;>上一页</a>";
}
if($start_page > 1) {
    echo "<a href=&#39;?page=1&#39;>1</a>";
    echo "<span>...</span>";
}
for($i=$start_page;$i<=$end_page;$i++) {
    echo "<a href=&#39;?page=$i&#39; ".($i==$page_num?&#39;class="active"&#39;:&#39;&#39;).">$i</a>";
}
if($total_page > $end_page) {
    echo "<span>...</span>";
    echo "<a href=&#39;?page=$total_page&#39;>$total_page</a>";
}
if($page_num < $total_page) {
    echo "<a href=&#39;?page=".($page_num+1)."&#39;>下一页</a>";
}
echo "</div>";

In the above code, we first need to calculate the total number of records and the total number of pages. Then, calculate the starting page number and ending page number based on the current page number, and put them into the paging navigation bar. Generate the required page numbers through a loop, and set the corresponding links and styles. Finally, we also need to determine whether we need to display ellipsis and previous page and next page links.

  1. Performance Optimization

When implementing the paging function, we also need to consider performance optimization. If you directly obtain all the data in the database and perform paging processing, it will lead to problems such as excessive memory consumption and slow query speed. Therefore, we need to use some techniques to improve the efficiency of paging queries, such as:

  • Use indexes: Using indexes in SQL statements can speed up queries.
  • Caching results: Use caching technology to cache query results and reduce the number of database queries.
  • Optimize SQL statements: Use keywords such as JOIN and WHERE to optimize SQL statements and reduce the size of query result sets.
  1. Third-party libraries

In addition to manually implementing the paging function, we can also use some third-party libraries to easily implement the paging function. For example, the Paginator class is built into the Laravel framework, which can easily implement the paging function. For specific usage, please refer to Laravel documentation.

Summary

This article introduces how PHP implements paging functions, including obtaining database data, displaying paging navigation bars, performance optimization, etc. In actual development, we also need to select the optimal solution based on specific scenarios to improve the efficiency and performance of paging queries.

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

Statement:
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