Virajsinh

Codeigniter Pagination Code

Dec 17th, 2019
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //Model Code
  3. //=============================================
  4. class Countries extends CI_Model
  5. {
  6.     public function __construct() {
  7.         parent::__construct();
  8.     }
  9.  
  10.     public function record_count() {
  11.         return $this->db->count_all("Country");
  12.     }
  13.  
  14.     public function fetch_countries($limit, $start) {
  15.         /*
  16.             $this->db->limit($limit, $start);
  17.             $this->db->select('*');
  18.             $this->db->from('Country');
  19.             $this->db->where('status', 'Active');
  20.             $query = $this->db->get();
  21.         */
  22.         $this->db->limit($limit, $start);
  23.         $query = $this->db->get("Country");
  24.  
  25.         if ($query->num_rows() > 0) {
  26.             foreach ($query->result() as $row) {
  27.                 $data[] = $row;
  28.             }
  29.             return $data;
  30.         }
  31.         return false;
  32.    }
  33. }
  34. ?>
  35. //=============================================
  36. //Controller Code
  37. //=============================================
  38. <?php
  39. public function blog()
  40.     {
  41.         $config = array();
  42.         $config["base_url"] = base_url() . "/user/blog/";
  43.         $config["total_rows"] = $this->blog_model->record_count();
  44.         $config["per_page"] = 10;
  45.         $config["uri_segment"] = 3;
  46.  
  47.         $config['full_tag_open'] = '<ul class="page-numbers">';
  48.         $config['full_tag_close'] = '</ul>';
  49.         $config['attributes'] = ['class' => 'page-number'];
  50.  
  51.         $config['first_link'] = false;
  52.         $config['last_link'] = false;
  53.  
  54.         $config['first_tag_open'] = '<li>';
  55.         $config['first_tag_close'] = '</li>';
  56.  
  57.         $config['prev_link'] = '<span class="ti-arrow-left"></span>';
  58.         $config['prev_tag_open'] = '<li>';
  59.         $config['prev_tag_close'] = '</li>';
  60.  
  61.         $config['next_link'] = '<span class="ti-arrow-right"></span>';
  62.         $config['next_tag_open'] = '<li>';
  63.         $config['next_tag_close'] = '</li>';
  64.  
  65.         $config['last_tag_open'] = '<li>';
  66.         $config['last_tag_close'] = '</li>';
  67.  
  68.         $config['cur_tag_open'] = '<li><span class="page-number current">';
  69.         $config['cur_tag_close'] = '</span></li>';
  70.  
  71.         $config['num_tag_open'] = '<li>';
  72.         $config['num_tag_close'] = '</li>';
  73.  
  74.         $this->pagination->initialize($config);
  75.         if($this->uri->segment(3)){
  76.             $page = ($this->uri->segment(3)) ;
  77.         }
  78.         else{
  79.             $page = 0;
  80.         }
  81.         $data["blogs"] = $this->blog_model->fetch_countries($config["per_page"], $page);
  82.         $data["links"] = $this->pagination->create_links();
  83.  
  84.         // View data according to array.
  85.         $this->load->view("Blog", $data);
  86.     }
  87. ?>
  88. //=============================================
  89. //View Code
  90. //=============================================
  91. <?php
  92. foreach($blogs as $data) {
  93.     echo $data->Name . " - " . $data->Continent . "<br>";
  94. }
  95. ?>
  96.    <p><?php echo $links; ?></p>
RAW Paste Data