Codeigniter Pagination with HMVC and mysql

How to create Codeigniter Pagination with HMVC and mysql :

This is the codeigniter pagination tutorial with hmvc and mysql database example with source code and you can use this code in Codeigniter MVC also.

In The Pagination Tutorial we will learn how to create our own pagination in codeigniter with HMVC structure by this tutorial you can integrate pagination very easily. If you are facing problem with codeigniter inbuilt pagination so you can do pagination by this tutorial. Here I have included source code also. You just download and change configuration and run from server.
First you have to create database pagination in mysql and create table users in the user table I took four fields but you can change as your feasibility:

  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `fullname` varchar(100) NOT NULL,
  `status` enum('Y','N') NOT NULL DEFAULT 'Y',
  PRIMARY KEY (`user_id`)

Insert data in user table

INSERT INTO `users` (`user_id`, `user_name`, `password`, `fullname`, `status`) VALUES
(1, 'smith', 'smith', 'Mr. Smith', 'Y'),
(2, 'allen', 'allen', 'Mr Allen', 'Y');

Very important thing in this script you have to make pagination_custom library inside library folder and write following code for pagination which will be call from your controller and load this library in your controller or inside config autoload.php.

Now we will create inside modules folder new module like pagination and inside this create MVC structure for this have to create controller,model,view folders and inside all create php files.
Inside module/pagination/controller create pagination.php and write below code for contoller:

class Pagination extends CI_Controller {
    public function __construct() {
    public function index() 
        $pagination = "";
        $total_records =  $this->pagination_model->users_count(); //count number of records
        $page = 1;
        $num_rec_per_page = 1;
        $start_from = 0;
        if(isset($_GET['page']) && $_GET['page'] > 1)
            $page  = $_GET["page"]; 
            $start_from = ($page-1) * $num_rec_per_page; 
            $previous  = $page-1;
            $next = $page+1;
            $page  = 1; 
            $start_from = ($page-1) * $num_rec_per_page; 
            $previous  = $page-1;
            $next = $page+1;
        if($total_records > $num_rec_per_page){
        $pagination = $this->pagination_custom->paginationCustom($page,$start_from,$num_rec_per_page,$total_records,$previous,$next);
        $data['userData'] = $this->load->pagination_model->getAllUsers($start_from,$num_rec_per_page);
        $data['pagination'] = $pagination;

If paginationCustom method gives error so please check your pagination library loaded or not.
Now we write model for record fetching pagination_model.php inside modules/pagination/model/:

class Pagination_Model extends CI_Model {
    public function __construct() {
     * Count All Users And Return Total rows count of users table
    public function users_count() {
        $count = $this->db->count_all_results();
        return $count;
    public function getAllUsers($start=0,$perpage=1) 
        $query = $this->db->get();
        //echo $this->db->last_query();
        return $query->result();

Now we write view for record display with pagination index.php inside modules/pagination/views/:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="">
<!-- Optional theme -->
<link rel="stylesheet" href="">
<!-- Latest compiled and minified JavaScript -->
<script src=""></script>
<div class="container">
    <div class="page-navi">
        <div class="page-navi-num">
            <?php echo $pagination; ?>
        <div class="display-count">
            <?php echo "Number Of Total Records - " . $total_records; ?>
    <div class="row">
        <div  class="col-sm-12">
            <div class="table-responsive">     
                <table  class="table table-striped table-bordered table-condensed" >
                    <th>S No</th>
                    <th>Full Name</th>
                    <th>User Name</th>
                        for ($i = 0; $i < count($userData); $i++) {
                                <td><?php echo $i+1; ?></td>
                                <td><?php echo $userData[$i]->fullname; ?></td>
                                <td><?php echo $userData[$i]->user_name; ?></td>
                        <?php } ?>

    <div class="clearfix visible-lg"></div>

For Download source Click Here
Please follow given instructions and get result.
Thank you.

  • Prabowo Agung

    tutorial bagus, mas mau tanya, untuk custome dengan bootstrap gmn ya mas?