Pagination in CodeIgniter (2026 Helpful Guide) – Complete Tutorial with Example

In this article, we’ll learn about Pagination In Codeigniter.

In the realm of web development, effective data navigation is paramount.

Pagination, a magical concept, allows users to gracefully traverse extensive datasets. CodeIgniter, a PHP framework celebrated for its elegance, equips developers with a plethora of tools to seamlessly implement pagination.

We’ll unveil the hidden gems of pagination in CodeIgniter.

Brace yourself for a unique journey where you’ll master the art of creating dynamic and intuitive paginated interfaces, captivating users and optimizing performance.

Pagination In Codeigniter

Pagination is one of the most frequently used features for web applications. Wherever we have a bunch of data and need to show them as a list, we require pagination to prevent hundreds/thousands of data in a single page. As a robust framework, Codeigniter provides a very efficient way to handle it with its integrated support. In this tutorial, we will see, how we can implement CodeIgniter pagination and enhance it further as per our needs.

CodeIgniter provides a built-in Pagination Library that simplifies implementation. It requires just a few configuration steps.

Core Steps:

  1. Load the pagination library
  2. Configure pagination settings
  3. Initialize pagination
  4. Fetch paginated data from the database
  5. Display data and links in the view

Step-by-Step Implementation

1. Load Pagination Library

$this->load->library('pagination');

2. Configure Pagination Settings

$config['base_url'] = base_url('index.php/default_page');
$config['total_rows'] = 100; // Total rows in database
$config['per_page'] = 10; // Records per page
$config['uri_segment'] = 3;

// Optional SEO enhancements
$config['reuse_query_string'] = TRUE;
$config['use_page_numbers'] = TRUE;

3. Initialize Pagination

$this->pagination->initialize($config);

4. Controller Example (Updated 2026)

public function index()
{
    $this->load->library('pagination');
    $this->load->model('my_model');

    $config['base_url'] = base_url('index.php/default_page');
    $config['total_rows'] = $this->my_model->count_all_data();
    $config['per_page'] = 10;
    $config['uri_segment'] = 3;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $data['my_result'] = $this->my_model->fetch_my_data($config['per_page'], $page);
    $data['links'] = $this->pagination->create_links();

    $this->load->view('my_view', $data);
}

5. Model Example

public function count_all_data()
{
    return $this->db->count_all('my_table');
}

public function fetch_my_data($limit, $start)
{
    $query = $this->db->limit($limit, $start)->get('my_table');
    return $query->result_array();
}

6. View File Example

<!DOCTYPE html>
<html>
<head>
<title>Pagination Example</title>
</head>
<body>

<h2>Data List</h2>

<?php foreach ($my_result as $row): ?>
<p><?php echo $row['name']; ?></p>
<?php endforeach; ?>

<div class="pagination-links">
<?php echo $links; ?>
</div>

</body>
</html>

Output Example

Pagination links will appear like:

« First < 1 2 3 4 5 > Last »

This code can be used to implement pagination in Codeigniter application.

Congratulations! You have unveiled the secrets of pagination in CodeIgniter.

Advanced Pagination (2026 Best Practices)

1. AJAX Pagination (Without Page Reload)

Use jQuery or Fetch API to load data dynamically:

$(document).on('click', '.pagination a', function(e) {
    e.preventDefault();
    var url = $(this).attr('href');

    $.get(url, function(data) {
        $('#content').html(data);
    });
});

2. SEO Optimization Tips

  • Use clean URLs (remove index.php using .htaccess)
  • Add rel=”next” and rel=”prev” tags
  • Avoid duplicate content
  • Use canonical URLs

3. Custom Styling (Bootstrap Example)

$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['cur_tag_open'] = '<li class="active"><span>';
$config['cur_tag_close'] = '</span></li>';

4. Accessibility Improvements

  • Add ARIA labels for navigation
  • Ensure keyboard navigation works
  • Use semantic HTML

Common Mistakes to Avoid

  • Not setting total_rows correctly
  • Incorrect uri_segment value
  • Loading all records instead of paginated results
  • Ignoring SEO-friendly URLs
  • Not handling empty results

Benefits of Pagination in CodeIgniter

  • Faster loading pages
  • Better database performance
  • Improved UI/UX
  • Scalable architecture
  • SEO-friendly structure

References

Conclusion

Pagination in CodeIgniter is a fundamental feature that every developer should master. With just a few lines of code, you can transform a cluttered data display into a clean, user-friendly interface.

In 2026, the focus goes beyond basic pagination—it’s about performance, SEO, and user experience. By implementing the techniques shared in this guide, you can build scalable and efficient applications that handle large datasets effortlessly.

With the knowledge gained on this extraordinary journey, you possess the power to enchant users with seamless data navigation and captivating interfaces.

Embrace the art of pagination in CodeIgniter, infuse your web applications with elegance, and guide users through the vast realms of data effortlessly. Unleash your magic, and may your paginated interfaces leave a lasting enchantment. Happy coding!

Hope this article helps!