As the content of the website continues to increase, it gradually becomes necessary to display it in pages. When using PHP to develop a website, how to implement paging display has become a basic skill. This article will introduce how to implement paging display in PHP.
1. The concept of paging display
Paging display is to divide a larger data result into multiple pages for display. For example, search results display, product display, etc. Paging display is generally divided into two methods: front-end paging and back-end paging.
Front-end paging: Front-end paging refers to slicing the entire data on the client side, and achieving the purpose of data paging through JavaScript display effect blocks. This approach usually extracts all the data at once (returned in JSON or XML format) and displays it via JavaScript.
Back-end paging: Back-end paging refers to paging data on the server side, returning only the data of the current page, not all data. This method usually determines the current page number based on the parameters passed in, and then uses SQL query parameters to only fetch the currently required data for display. The most commonly used is the Limit statement provided in MySQL, which can easily implement the paging function, allowing us to easily perform data paging in PHP.
2. Use PHP and MySQL to implement paging
Let’s introduce in detail how to use PHP and MySQL to implement paging function.
Before we begin, we need to connect to the MySQL database. You can use PDO or mysqli to connect to the database.
//连接到MySQL数据库 $host = "localhost"; $user = "username"; $password = "password"; $database = "database"; $connect = mysqli_connect($host, $user, $password, $database);
?>
In order to implement paging, we first need to get the query The total number of records in the results for paging purposes. We can get the total number of records through SQL statements.
// 获取记录总数 $sql = "SELECT COUNT(*) FROM table_name"; $result = mysqli_query($connect, $sql); $row = mysqli_fetch_row($result); $total = $row[0];
?>
Display in paging Previously, we needed to specify the number of records to display per page. Typically 10 or 20 records are displayed per page, but we can adjust this as needed.
//设定每页显示的记录数 $per_page = 10;
?>
Get the total number of records and each After the number of records displayed on the page, we can calculate how many pages there are in total.
// 计算总页数 $total_pages = ceil($total / $per_page);
?>
Users can navigate to the desired page by entering the page number required page. Therefore, we need to get the current page number first (if not specified, it defaults to page 1).
// 获取当前页码 if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
?>
Before executing the query, we need Figure out which record the page should start with. For the first page, we start from the first record; for other pages, we need to calculate the starting record position of the page through the current page number.
// 计算起始记录位置 $start = ($page - 1) * $per_page;
?>
After getting the starting record position, we You can execute the query and output the results to an HTML document. The Limit statement in MySQL is used here.
// 查询数据并显示 $sql = "SELECT * FROM table_name LIMIT $start, $per_page"; $result = mysqli_query($connect, $sql); while ($row = mysqli_fetch_assoc($result)) { echo $row['column_name']; }
?>
The last step is to create paginated navigation so that users can Quickly jump to the page you want. Common pagination navigation includes numeric links and "previous page" and "next page" links. We can choose the appropriate paging navigation according to our needs.
// 分页链接 if ($total_pages > 1) { echo ''; if ($page > 1) { echo '上一页'; } for ($i = 1; $i <= $total_pages; $i++) { if ($i == $page) { echo ''.$i.''; } else { echo ''.$i.''; } } if ($page < $total_pages) { echo '下一页'; } echo ''; }?>
The above is an abbreviated version of paging using PHP and MySQL.
3. Problems that may be encountered in paging
You may encounter the following problems in the process of implementing paging:
- The methods of obtaining the total number of records are different: in MySQL A common way to get the total number of records is to use the COUNT statement, which may not be accurate enough in some cases.
- The query process is slow: If the amount of data is very large, paging query may cause the query process to be slow. In this case, methods such as caching or data sharding can be used to optimize query efficiency.
- SQL injection vulnerability: If the page number or other parameters entered by the user are not filtered, it may lead to SQL injection vulnerability. Data filtering and verification should be performed in the code.
4. Summary
Through the above introduction, we can clearly understand how to implement paging in PHP. We can also see that the method of implementing paging will vary depending on the database and database used. The actual scenarios vary, but the general process is the same. Students can think about it themselves and use different methods to improve our paging operation experience and efficiency to achieve a better user experience.
The above is the detailed content of How to implement paging display in php. For more information, please follow other related articles on the PHP Chinese website!