Site Admin
Site Admin Founder of MeaningArticles

Codeigniter Pagination Example

Hello Dev.

in this academic, you will learn how to paginate database results in CodeIgniter the usage of the pagination library. This article presumes which you are familiar with the basics of CodeIgniter Active record. the opposite assumption made is which you have already downloaded CodeIgniter.

let's start one by one follow bellow article.

Database configuration

We will start with creating the database and inserting some dummy records in it. It will be a single table database with 15 records on it.

Run the following script against MySQL to create the database authors table. Insert 15 dummy records.

CREATE SCHEMA ci_pagination;


CREATE TABLE `authors` (
  `first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `birthdate` date NOT NULL,
  `added` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (1, 'James', 'Butt', '[email protected]', '1944-07-23', '2000-08-18 18:16:29');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (2, 'Josephine', 'Darakjy', '[email protected]', '1981-12-13', '1999-08-07 00:25:09');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (3, 'Art', 'Venere', '[email protected]', '1984-12-29', '2016-03-08 13:55:40');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (4, 'Lenna', 'Paprocki', '[email protected]', '1979-01-28', '1993-09-11 00:11:31');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (5, 'Donette', 'Foller', '[email protected]', '1996-05-18', '2016-03-20 06:56:49');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (6, 'Simona', 'Morasca', '[email protected]', '1995-08-02', '1997-07-07 23:33:13');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (7, 'Mitsue', 'Tollner', '[email protected]', '1991-01-18', '2015-05-21 10:47:10');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (8, 'Leota', 'Dilliard', '[email protected]', '1991-10-04', '2005-05-05 08:18:38');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (9, 'Sage', 'Wieser', '[email protected]', '1998-11-07', '2008-07-20 22:20:24');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (10, 'Kris', 'Marrier', '[email protected]', '2000-11-05', '2006-02-12 21:50:47');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (11, 'Butt', 'James', '[email protected]', '1945-08-25', '1999-07-16 17:15:21');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (12, 'Darakjy', 'Josephine', '[email protected]', '1982-02-23', '1998-07-07 00:26:10');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (13, 'Paprocki', 'Lenna', '[email protected]', '1990-02-27', '1994-08-12 00:19:33');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (14, 'Donette', 'Tollner', '[email protected]', '1997-12-31', '1999-08-31 10:09:03');
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (15, 'Simona', 'Dilliard', '[email protected]', '2000-02-12', '2004-06-23 13:05:26');

Now that we have created our database successfully and inserted dummy records in it, let's configure our CodeIgniter application to communicate with the database.

Open application/config/database.php

Set the database connection parameters similar to the following


$db['default'] = array(
    'dsn'    => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'melody',
    'database' => 'ci_pagination',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE

We will also be autoloading the database library when our application starts. Let's do that. Open application/config/autoload.php

Load the database library as shown by the code below

$autoload['libraries'] = array('database');

That's it for the database configuration.

Let's now work on the database model for pagination

CodeIgniter Pagination Database Model

Create a new model Authors_model in application/models

Add the following code


class Authors_model extends CI_Model {

    protected $table = 'authors';

    public function __construct() {

    public function get_count() {
        return $this->db->count_all($this->table);

    public function get_authors($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->table);

        return $query->result();

that's it for our database model. Let's now create the routes that will be responding to our paginated results.

CodeIgniter Pagination Routes

Open the routes file in application/config/routes.php

Add the following route

$route['authors/(:num)'] = 'authors';

Let's now move on to the controller for our paginated results

CodeIgniter Pagination Controller

Create a new file Authors.php in application/controllers directory

Add the following code

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Authors extends CI_Controller {

    public function __construct() {
        parent:: __construct();


    public function index() {
        $config = array();
        $config["base_url"] = base_url() . "authors";
        $config["total_rows"] = $this->authors_model->get_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 2;


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

        $data["links"] = $this->pagination->create_links();

        $data['authors'] = $this->authors_model->get_authors($config["per_page"], $page);

        $this->load->view('authors/index', $data);

That's it for our model. Let's now create the view that will display our database results.

Create a new directory authors in application/views

Create a new file index.php in application/views/authors/index.php

Add the following code

<!DOCTYPE html>
        <title>CodeIgniter Pagination -</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="">
        <div class="container">
            <h3 class="title is-3">CodeIgniter Database Pagination -</h3>
            <div class="column">
                <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                            <th>Contact Name</th>
                            <th>Contact Number</th>
                        <?php foreach ($authors as $author): ?>
                                <td><?= $author->id ?></td>
                                <td><?= $author->first_name ?></td>
                                <td><?= $author->last_name ?></td>
                                <td><?= $author->email ?></td>
                                <td><?= $author->birthdate ?></td>
                        <?php endforeach; ?>
                <p><?php echo $links; ?></p>

In this tutorial, we are using the built-in PHP web server, but you can use any web server that supports PHP.

Open the terminal

Run the following command

cd C:\Sites\ci-app
php -S localhost:3000

Note: the application path has to match the path where you downloaded CodeIgniter. For that, you can use any port number that is free on your computer. It's not necessary to use port 3000.

Open the web browser and load the following URL


i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.