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.
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.
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;
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;
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 结果"; }
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> "; }
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> "; }
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!