Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Pagination
Pagination in PHP refers to the process of dividing a large set of data into smaller, more manageable sections called "pages." It is commonly used in web applications to display a limited number of records or results per page, allowing users to navigate through the data easily.
Pagination is essential when dealing with large data sets because displaying all the records on a single page can lead to performance issues and an overwhelming user interface. By implementing pagination, you can improve the user experience and optimize the performance of your application.
How Pagination Works
Database Setup Required: Create a MySQL database with a table named "studentdata" containing columns: id, name, age. Insert sample records to test pagination.
Basic Pagination Implementation
Here's a complete PHP pagination example that displays 10 records per page from a student database
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
margin-bottom: 20px;
}
.grid-item {
background-color: #f2f2f2;
padding: 10px;
text-align: center;
border: 1px solid #ddd;
}
.grid-item strong {
background-color: #4CAF50;
color: white;
padding: 5px;
display: block;
}
.pagination {
display: inline-block;
margin: 20px 0;
}
.pagination a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
border: 1px solid #ddd;
margin: 0 2px;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
.pagination a:hover:not(.active) {
background-color: #ddd;
}
</style>
</head>
<body>
<h2>Student Data with Pagination</h2>
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "assignments";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Pagination variables
$recordsPerPage = 10;
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$startFrom = ($currentPage - 1) * $recordsPerPage;
// Fetch records for current page
$sql = "SELECT * FROM studentdata LIMIT $startFrom, $recordsPerPage";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<div class='grid-container'>";
echo "<div class='grid-item'><strong>ID</strong></div>";
echo "<div class='grid-item'><strong>Name</strong></div>";
echo "<div class='grid-item'><strong>Age</strong></div>";
while ($row = $result->fetch_assoc()) {
echo "<div class='grid-item'>" . htmlspecialchars($row["id"]) . "</div>";
echo "<div class='grid-item'>" . htmlspecialchars($row["name"]) . "</div>";
echo "<div class='grid-item'>" . htmlspecialchars($row["age"]) . "</div>";
}
echo "</div>";
} else {
echo "<p>No records found.</p>";
}
// Calculate total pages
$countSql = "SELECT COUNT(*) AS total FROM studentdata";
$countResult = $conn->query($countSql);
$totalRecords = $countResult->fetch_assoc()["total"];
$totalPages = ceil($totalRecords / $recordsPerPage);
// Generate pagination links
if ($totalPages > 1) {
echo "<div class='pagination'>";
// Previous link
if ($currentPage > 1) {
echo "<a href='?page=" . ($currentPage - 1) . "'>« Previous</a>";
}
// Page numbers
for ($i = 1; $i <= $totalPages; $i++) {
$activeClass = ($i == $currentPage) ? "active" : "";
echo "<a class='$activeClass' href='?page=$i'>$i</a>";
}
// Next link
if ($currentPage < $totalPages) {
echo "<a href='?page=" . ($currentPage + 1) . "'>Next »</a>";
}
echo "</div>";
echo "<p>Page $currentPage of $totalPages (Total: $totalRecords records)</p>";
}
$conn->close();
?>
</body>
</html>
Key Components
| Component | Purpose | Example |
|---|---|---|
| LIMIT clause | Restricts query results | LIMIT 0, 10 |
| Page calculation | Determines starting record | (page-1) × records_per_page |
| Total pages | Calculates navigation links | ceil(total_records / per_page) |
How It Works
The pagination system works by using the SQL LIMIT clause to fetch only a subset of records. The starting position is calculated using ($currentPage - 1) * $recordsPerPage. For page 1, it starts from record 0; for page 2, it starts from record 10, and so on.
Navigation links are generated dynamically based on the total number of records and records per page. The current page is highlighted, and Previous/Next buttons provide easy navigation.
Conclusion
PHP pagination improves performance and user experience by displaying data in manageable chunks. Using SQL's LIMIT clause with proper page calculations creates an efficient pagination system. Always validate page parameters and use proper HTML escaping for security.
