Pagination inarahisisha kuonesha data nyingi kwenye table kwa hatua ndogo (pages), ikiboresha user experience na kupunguza loading time. Hii ni muhimu kwa dashboards, reports, na list za records.

Hatua kwa Hatua
1️⃣ Unda Database Table (Mfano)
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);

-- Ongeza sample data
INSERT INTO users (name, email) VALUES
('Faustine','faustine@example.com'),
('Amina','amina@example.com'),
('Kwaku','kwaku@example.com');

2️⃣ PHP Pagination Logic
<?php
$conn = new mysqli("localhost", "root", "", "test_db");

// Number of records per page
$limit = 5;

// Current page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// Total records
$result = $conn->query("SELECT COUNT(*) AS total FROM users");
$row = $result->fetch_assoc();
$total = $row['total'];
$pages = ceil($total / $limit);

// Fetch records for current page
$result = $conn->query("SELECT * FROM users LIMIT $start, $limit");
?>

3️⃣ Display Table with Pagination
<table border="1" cellpadding="10">
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
<?php while($user = $result->fetch_assoc()): ?>
<tr>
<td><?= $user['id'] ?></td>
<td><?= $user['name'] ?></td>
<td><?= $user['email'] ?></td>
</tr>
<?php endwhile; ?>
</table>

<div class="pagination">
<?php for($i=1; $i<=$pages; $i++): ?>
<a href="?page=<?= $i ?>"
style="margin:0 5px; <?= $i==$page?'font-weight:bold;':'' ?>"><?= $i ?></a>
<?php endfor; ?>
</div>

4️⃣ Faida za PHP Pagination

Improves UX: Hakuna page inabebwa na long tables.

Performance: Load data kidogo kwa page, inasababisha fast loading.

Reusable: Logic inaweza kutumika kwenye table yoyote.

SEO Friendly: Page numbers zinaonekana kwenye URL (?page=1).

🔗 Links Za Kujifunza Zaidi

🌐 Faulink Official Website: https://www.faulink.com/

📘 Jifunze Web Design & Programming (Tutorials / Mifumo): https://www.faulink.com/excel_mifumo.php

📲 Piga / WhatsApp kwa msaada wa haraka: https://wa.me/255693118509