Pagination is a technique used in web development to divide a large set of data into smaller, more manageable parts or pages. It is commonly used when displaying lists, tables, or search results that contain a large number of items.
Step-1:
First Create table
CREATE TABLE IF NOT EXISTS pagination_users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
Step-2:
Insert Some records in table
INSERT INTO pagination_users (name, email) VALUES
('John Doe', 'john@example.com'),
('Jane Smith', 'jane@example.com'),
('Alice Johnson', 'alice@example.com'),
('Bob Brown', 'bob@example.com'),
('Eve Taylor', 'eve@example.com'),
('Michael Clark', 'michael@example.com'),
('Emily White', 'emily@example.com'),
('David Lee', 'david@example.com'),
('Sarah Hall', 'sarah@example.com'),
('Chris Adams', 'chris@example.com');
Step-3:
Database Connection (connection.php)
<?php
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Replace 'servername
', 'username
', 'password
', and 'dbname
' with your actual database credentials and table name.
Step-4:
Pagination Script (index.php)
<?php include('connection.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Pagination</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
// Pagination settings
$results_per_page =10; // Number of records per page
// Determine current page
if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
$page = 1;
} else {
$page = intval($_GET['page']);
}
// Calculate starting point for records
$start_from = ($page - 1) * $results_per_page;
// Fetch records from the database
$sql = "SELECT * FROM pagination_users LIMIT $start_from, $results_per_page";
$result = $conn->query($sql);
// Display records in a table
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['email']}</td></tr>";
}
echo "</table>";
} else {
echo "No records found.";
}
// Pagination links
$sql = "SELECT COUNT(*) AS total FROM pagination_users";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page);
echo "<div class='pagination'>";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=$i' class='" . ($page == $i ? "active" : "") . "'>$i</a>";
}
echo "</div>";
// Close connection
$conn->close();
?>
</body>
</html>
Step-5:
CSS File (styles.css)
<style>
body {
font-family: Arial, sans-serif;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.pagination {
margin-top: 20px;
}
.pagination a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
border: 1px solid #ddd;
margin: 0 4px;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
.pagination a:hover:not(.active) {background-color: #ddd;}
<style>