Home > Backend Development > PHP Tutorial > How to use PHP to develop a simple data paging function

How to use PHP to develop a simple data paging function

PHPz
Release: 2023-09-25 08:14:02
Original
1375 people have browsed it

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:

  1. Connect to the database:
    First you need to create a connection function to the database and fill in the correct database connection information in the function. The code example is as follows:

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;
Copy after login

}
?>

  1. Get data Total number:
    Next, you need to write a function to get the total number of records in the data table. The code example is as follows:

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;
Copy after login

}
?>

  1. Paging query Data:
    Next, you need to write a function to query the corresponding data based on the page number and the number of records displayed on each page. The code example is as follows:

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;
Copy after login

}
?>

  1. Display paging navigation:
    Finally, you need to write a function to display paging navigation to facilitate users to switch between different page numbers. The code example is as follows:

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>";
Copy after login

}
?>

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']);
Copy after login

}

// 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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template