PHP CodeIgniter Pagination with Twitter Bootstrap Styles - Example

Implement Twitter Bootstrap Styles in CodeIgniter Pagination

Create new model in codeigniter name Pagination_model.php

put this code in this model

<?php

class Pagination_model extends CI_Model {
 
 public function __construct() 
 {
   parent::__construct();
 }
 
  function record_count( $where = array() , $table , $order_by = array()){
  
  $row = $this->db->where($where)->order_by(key($order_by) , $order_by[key($order_by)])->get($table);
        return $row->num_rows();
 }
 
 public function fetch_data($limit, $start , $where = array() , $table , $order_by = array()) {
   
  $query = $this->db->where( $where )->order_by(key($order_by) , $order_by[key($order_by)])->limit($limit, $start)->from($table)->get();

        if($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
 }
 
 function get_paginaion($arg){

  $this->load->helper("url");        
  $this->load->library("pagination");  
  $config = array();  
  $config['full_tag_open'] = "<ul class='pagination'>";  
  $config['full_tag_close'] ="</ul>";  
  $config['num_tag_open'] = '<li>';  
  $config['num_tag_close'] = '</li>';  
  $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";  
  $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";  
  $config['next_tag_open'] = "<li>";  
  $config['next_tagl_close'] = "</li>";  
  $config['prev_tag_open'] = "<li>";  
  $config['prev_tagl_close'] = "</li>";  
  $config['first_tag_open'] = "<li>";  
  $config['first_tagl_close'] = "</li>";  
  $config['last_tag_open'] = "<li>";  
  $config['last_tagl_close'] = "</li>";           
  $config["base_url"] = base_url() . $arg["url"];        
  $config["total_rows"] = $this->record_count($arg['where'] , $arg['table'] , $arg['order_by']);        
  $config["per_page"] = $arg["per_page"];        
  $config["uri_segment"] = $arg["uri_segment"];    
  $this->pagination->initialize($config);  
  $page = ($this->uri->segment($arg["uri_segment"])) ? $this->uri->segment($arg["uri_segment"]) : 0;     
  $data["table_data"] = $this->fetch_data($config["per_page"], $page , $arg['where'] , $arg['table'] , $arg['order_by'] ); 
  $data["links"] = $this->pagination->create_links();
  $data["per_page"] = $arg["per_page"];
  
  $data["current_page"] = ($page > 1) ? $page : 0;
  return $data;
 }

}

?>

now create controller in codeigniter Pagination.php

put this code in the controller

<?php
/**
* Author: Amir shahzad
*     amir.shahzad.mcs@gmail.com
*
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Pagination.php extends CI_Controller {
 function __construct(){
        parent::__construct();
  $this->load->library(array('login_admin_library','form_validation'));
  $this->load->helper(array('url','language'));
  $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'login_admin_library'), $this->config->item('error_end_delimiter', 'login_admin_library'));
  
    }
 function index($id = null){
  
  $data = array();
  $this->load->model("ci-admin/user-manage/user_model");
  $this->data["page_name"] = "Manage users";
  $this->data['number_of'] =  $this->user_model->count_user();
  
  if ($this->login_admin_library->logged_in()){
   
   $this->data["page_name"] = "Manage users";
   $this->load->model("pagination/pagination_model");

   $arg = array(
     "where"   => array("status" => 0), 
     "order_by"   => array("id" => "ASC"),  
     "table"   => "users",
     "per_page"   => 20,       
     "uri_segment"  => 5,   
     "url"    => "ci-admin/user-manage/user/index/",
    );
   $this->pagination = $this->pagination_model->get_paginaion($arg);
   $this->data["users"] = $this->pagination["table_data"];
   $this->data["links"] = $this->pagination["links"];
   
   $this->load->view("ci-admin/include/header" , $this->data);
   $this->load->view("ci-admin/include/side-bar");
   $this->load->view("ci-admin/user-manage/manage-users" , $this->data);
   $this->load->view("ci-admin/include/footer");
  
  }else{
   set_flash("message" , "ok");
   ci_send("ci-admin/login");
  }
  
 }

}

now go to the your view which name is manage-users.php

write this is your view

<div>
     <table id="dynamic-table" class="table table-striped table-bordered table-hover">
      
      
      
      <thead>
       <tr>
        <th>Full name</th>
        <th>Email Address</th>
        <th>IP Address</th>
        <th>Active / Deactive</th>
        <th>Action</th>
       </tr>
      </thead>

      <tbody>
       <?php if(is_array($users)){?>
       <?php foreach($users as $user){?>
        <tr <?php echo ($user->active == 1) ? "style='background-color:#FDFDFD;'" :"style='background-color:#EFEEEE;'" ; ?> >
         
         <td>  <?php if($user->user_roles == 5){ echo $user->first_name." ".$user->last_name; }else{ echo $user->username; } ?>   </td>
         <td> <?php echo $user->email;?>          </td>
         <td> <?php echo $user->ip_address;?>         </td>
         <td> <?php echo ($user->active == 1 )? "Active" : "Deactive";?>  </td>

         <td > 
          <div class="hidden-sm hidden-xs action-buttons">
           

           <?php if($user->active == '0'){?>
            <a href="<?php echo base_url()."ci-admin/user-manage/activeDeactive/activate_user/".$user->id;?>"  title="Now user deactive. click to active user"  class="red" style="margin-bottom: 5px; margin-top: 10px;"> <i class="fa fa-power-off bigger-130 icon-red"></i></a>
           <?php }else{ ?> 
            <a href="<?php echo base_url()."ci-admin/user-manage/activeDeactive/deactivate_user/".$user->id;?>"  title="Now user active. click to deactive user" style="margin-bottom: 5px; margin-top: 10px;"> <i class="fa fa-check-circle-o bigger-130"></i></a>
           <?php }?>
           <a class="red delete-item" mesg="" href="<?php echo base_url()."ci-admin/user-manage/deleteUser/deleted/".$user->id;?>">
            <i class="ace-icon fa fa-trash-o bigger-130"></i>
           </a>
          </div>
         </td>
        
        </tr>
       <?php } ?>
       <?php }else{ ?>
         
         <tr >
         
          <td colspan="6"><h1 class="blur"> No user found</h1></td>
         
         </tr>
         
       <?php } ?>
      </tbody>
     </table>
     <p><?php echo $links;?></p>
    </div>

Share this

Related Posts

Previous
Next Post »