🔴 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();
$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 LIMIT and OFFSET to 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();
$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)।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

Is page ki language (PHP) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.
Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.