Home > Article > Backend Development > How to use PHP to develop a simple data paging function
How to use PHP to develop a simple data paging function
Introduction:
In website development, we often encounter situations where a large amount of data needs to be displayed in paging. In this case, you need to use the data paging function. This article will introduce how to use PHP to develop a simple data paging function and provide specific code examples.
1. Preparation work:
Before you start writing code, you need to prepare a database and a data table to store the data that needs to be displayed in pages. Taking the MySQL database as an example, create a data table named "users" with the following table structure:
CREATE TABLE users
(
id
int (11) NOT NULL AUTO_INCREMENT,
name
varchar(50) NOT NULL,
age
int(11) NOT NULL,
PRIMARY KEY ( id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. Write the paging function code:
function connectDB() {
$servername = "localhost"; $username = "root"; $password = "password"; $dbname = "database"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } return $conn;
}
?>
function getTotalCount() {
$conn = connectDB(); $sql = "SELECT COUNT(*) AS count FROM users"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $count = $row["count"]; $conn->close(); return $count;
}
?>
function getData($page, $pageSize) {
$conn = connectDB(); $start = ($page - 1) * $pageSize; $sql = "SELECT * FROM users LIMIT $start, $pageSize"; $result = $conn->query($sql); $data = array(); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $data[] = $row; } } $conn->close(); return $data;
}
?>
function showPagination($page, $pageSize, $totalCount) {
$totalPage = ceil($totalCount / $pageSize); $prevPage = $page - 1; $nextPage = $page + 1; echo "<div class='pagination'>"; if ($page > 1) { echo "<a href='?page=$prevPage'>上一页</a>"; } for ($i = 1; $i <= $totalPage; $i++) { if ($i == $page) { echo "<span class='current'>$i</span>"; } else { echo "<a href='?page=$i'>$i</a>"; } } if ($page < $totalPage) { echo "<a href='?page=$nextPage'>下一页</a>"; } echo "</div>";
}
?>
3. Use the paging function:
Integrate the above function code into a PHP file, and add the following code to the file to display the paging function:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = 10;
$totalCount = getTotalCount();
$data = getData($page, $pageSize);
// Display data
foreach ($data as $row) {
echo sprintf("ID:%s,姓名:%s,年龄:%s<br>", $row['id'], $row['name'], $row['age']);
}
// Display paging Navigation
showPagination($page, $pageSize, $totalCount);
?>
By accessing the PHP file, you can see the data displayed in paging and the paging navigation.
Conclusion:
Through the above steps, we successfully developed a simple data paging function using PHP and gave specific code examples. Readers can modify and expand the code according to actual needs to achieve more complex data paging functions. I hope this article can provide some reference and help for everyone to use the data paging function in website development.
The above is the detailed content of How to use PHP to develop a simple data paging function. For more information, please follow other related articles on the PHP Chinese website!