agyanadda@gmail.com

CodeIgniter 4 Pagination

Database and Models 1 min read Updated Dec 3, 2026 Topic 16 of 30

Showing hundreds of rows on one page is slow. Pagination splits a list into pages.

With a model

Replace findAll() with paginate(10). It reads the ?page= value from the URL and returns only that page's rows.

Pass the pager to the view: 'pager' => $model->pager.

Showing the links

  • <?= $pager->links() ?> — full links (First, 1 2 3, Last)
  • <?= $pager->simpleLinks() ?> — just Older / Newer

Filters and search

Add conditions before paginate(): $model->like('title', $q)->paginate(10). To keep the search term in page links, use $pager->only(['q'])->links().

Custom look (Bootstrap)

Create a template, for example app/Views/pagers/bootstrap.php, register it in app/Config/Pager.php under $templates, and use it: $pager->links('default', 'bootstrap').

More than one list

Give each list a group name: paginate(10, 'posts') and $pager->links('posts').

Useful pager methods: getCurrentPage(), getPageCount(), getTotal().

Example

Example — Paginated search PHP
<?php
// app/Controllers/Posts.php
public function index()
{
    $model = model(PostModel::class);
    $q     = trim((string) $this->request->getGet('q'));

    if ($q !== '') {
        $model->like('title', $q);
    }

    return view('posts/index', [
        'posts' => $model->orderBy('id', 'DESC')->paginate(10),
        'pager' => $model->pager,
        'q'     => $q,
    ]);
}

// app/Views/posts/index.php
// <?php foreach ($posts as $post): ?>
//   <h3><?= esc($post['title']) ?></h3>
// <?php endforeach ?>
// <?= $pager->only(['q'])->links() ?>
// <p>Page <?= $pager->getCurrentPage() ?> of <?= $pager->getPageCount() ?></p>

/posts?q=php&page=2 shows the second page of posts with "php" in the title. Page links keep q=php, and the text shows e.g. "Page 2 of 4".

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which model method returns one page of results?

Exercise 2 True / False

$pager->simpleLinks() shows only Older and Newer links.

Exercise 3 Fill in the Blank

Keep the q search value in page links with $pager->___(['q'])->links()

Frequently Asked Questions

Copy the default pager view from the framework (system/Pager/Views/default_full.php) to app/Views/pagers/bootstrap.php, add Bootstrap classes such as pagination and page-item, register it in app/Config/Pager.php and call $pager->links('default', 'bootstrap').