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:

CREATE TABLE IF NOT EXISTS `users` (
  `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`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

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:

<?php
class Pagination extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->model('pagination_model');
    }
    
    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;
        }
        else
        {
            $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;
        $data['total_records']=$total_records;
        $this->load->view('pagination/index',$data);        
    } 
}

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/:

<?php
class Pagination_Model extends CI_Model {
    
    public function __construct() {
        parent::__construct();
        //$this->load->database();
    }
    
    /*
     * Count All Users And Return Total rows count of users table
     */
    public function users_count() {
        $this->db->from('users');
        $count = $this->db->count_all_results();
        return $count;
    }
    
    
    public function getAllUsers($start=0,$perpage=1) 
    {
        $this->db->select("*");
        $this->db->from("users");
        $this->db->limit($perpage,$start);
        $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/:

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

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

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?