Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set paging in Codelgniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 192
    Comment on it

    Hello Friends,

    If you are looking to set paging and listing in CodeIgniter. Please review the below example code and make your changes accordingly.

     

    Controller Code

    Open your controller file and make the changes as below:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Lands extends CI_Controller {           // Please change the class name
      public function __construct() {
            parent:: __construct();
            $this->load->helper("url");
            $this->load->model("Landinfo");       // Define your model name
            $this->load->library("pagination");   // Include paging library
        }
    
        public function index() {
            $config = array();
            $config["base_url"] = base_url() . "lands/index";         // define your page path
            $config["total_rows"] = $this->Landinfo->record_count();
            $config["per_page"] = 20;                                          
            $config["uri_segment"] = 3;
    
            $this->pagination->initialize($config);
    
            $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data["results"] = $this->Landinfo->fetch_lands($config["per_page"], $page);
            $data["links"] = $this->pagination->create_links();
            $this->load->view("lands", $data);
        }
    
    ?>

     

    Model Code

    Open your model file and make the changes as below:

    <?php
    
    // Change the class name as per your requirement
    class Landinfo extends CI_Model      
    {
        public function __construct() {
            parent::__construct();
        }
    
       /* count number of records */
    
        public function record_count() {
            return $this->db->count_all("landinfo");
        }
    
        public function fetch_lands($limit, $start) {
            // define paging
    
            $this->db->limit($limit, $start);
            $this->db->order_by('id', 'desc');
            $query = $this->db->get("landinfo");
    
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $data[] = $row;
                }
                return $data;
            }
            return false;
       }
    
    ?>
    
    
    // View File
    
    
    <table id="t01">
      <tr>
          <td><strong>Holder Name</strong></td>
          <td><strong>Crop Type</strong></td>
          <td><strong>Khata Number</strong></td>
          <td><strong>Khasra Number</strong></td>
          <td><strong>Season</strong></td>
      </tr>
            <?php
            foreach($results as $data) { ?>
                <tr>
                      <td><?php echo $data->holder_name; ?></td>
                      <td><?php echo $data->crop_type; ?></td>
                      <td><?php echo $data->khata_no; ?></td>
                      <td><?php echo $data->khasra_no; ?></td>
                      <td><?php echo $data->season; ?></td>
                      <td><a href="<?php echo base_url(); ?>/lands/viewdetail/index?id=<?php echo $data->id; ?>">View Detail</td>
                </tr>      
            ?>
    
    <!--------------- The below code will show my PAGING -->
            <tr>
                   <td colspan="5" align="center"><?php echo $links; ?></td>
               </tr>
       </table>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: