🔴 Advanced · Lesson 34
Pagination
Pagination
What is Pagination?
Pagination splits a long list of records into pages (like Page 1, 2, 3) so you do not load thousands of rows at once.
Pagination Logic
$page = $_GET["page"] ?? 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare("SELECT * FROM students LIMIT ? OFFSET ?");
$stmt->execute([$perPage, $offset]);
$rows = $stmt->fetchAll();
Page Links
$total = $pdo->query("SELECT COUNT(*) FROM students")->fetchColumn();
$pages = ceil($total / $perPage);
for ($i = 1; $i <= $pages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}
Summary
- Pagination uses
LIMITandOFFSETto fetch one page at a time. - OFFSET = (page - 1) × perPage; total pages = ceil(total / perPage).
Pagination क्या है?
Pagination records की लंबी list को pages में बाँटता है (जैसे Page 1, 2, 3) ताकि हज़ारों rows एक साथ load न हों।
Pagination Logic
$page = $_GET["page"] ?? 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare("SELECT * FROM students LIMIT ? OFFSET ?");
$stmt->execute([$perPage, $offset]);
$rows = $stmt->fetchAll();
Page Links
$total = $pdo->query("SELECT COUNT(*) FROM students")->fetchColumn();
$pages = ceil($total / $perPage);
for ($i = 1; $i <= $pages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}
सारांश
- Pagination
LIMITऔरOFFSETसे एक बार में एक page लाता है। - OFFSET = (page - 1) × perPage; total pages = ceil(total / perPage)।