azazqadir azazqadir - 11 months ago
1352 0

Creating Model, Controller and View for a Pagination System in CodeIgniter Application. Source: https://www.cloudways.com/blog/pagination-in-codeigniter/

PHP

models/departments.php

<?php

class Departments extends CI_Model

{

   public function __construct() {

       parent::__construct();

   }



   public function record_count() {

       return $this->db->count_all("Departments");

   }



   public function fetch_departments($limit, $start) {

       $this->db->limit($limit, $start);

       $query = $this->db->get("Departments");



       if ($query->num_rows() > 0) {

           foreach ($query->result() as $row) {

               $data[] = $row;

           }

           return $data;

       }

       return false;

   }

}
PHP

welcome.php

<?php

class Welcome extends CI_Controller

{

   public function __construct() {

       parent:: __construct();

       $this->load->helper("url");

       $this->load->model("Departments");

       $this->load->library("pagination");

   }

   public function departmentdata() {

       $config = array();

       $config["base_url"] = base_url() . "welcome/departmentdata";

       $config["total_rows"] = $this->Departments->record_count();

       $config["per_page"] = 5;

       $config["uri_segment"] = 3;

       $this->pagination->initialize($config);

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

       $data["results"] = $this->Departments->

           fetch_departments($config["per_page"], $page);

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

       $this->load->view("departmentdata", $data);
   }
}
PHP

views/departmentdata.php

<body>

<div id="container">

   <div id="body">

       <?php

       foreach($results as $data) {

           echo $data->Departments . " - " . $data->Labs . "<br>";

       }

       ?>

       <p><?php echo $links; ?></p>

   </div>



</div>

</body>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download