Amazon Best Products with minimum price

Monday 3 April 2017

How to use Pagination with Codeigniter ??

This Blog Post is about : How to use pagination in codeigniter Framework?

Codeigniter has its own library pagination which is used for enable pagination on the table.
Pagination is used for the displaying data one by one in page wise (For example: If there are 100 records in table and you want to display it in a page, thereby it takes the whole page and users keep only scrolling down for the record.Instead of scrolling down the page, Pagination divides the record into multiple pages and provides the link. So user can jump direct to any one page where he wants to.)

In this tutorial, we are going to fetch data from database and display it in "View" after that we will write a code in controller of pagination and display the same data in table pagination format.

1) First we are going to create pages in "View". Let's create the task.php in application/view and load the CDN of bootstrap and Jquery in <head> tag in html page.

<!doctype html>
<html lang = "en">
<head>
  <meta charset = "utf-8">
  <title>Pagination</title>
  <script src = "https://code.jquery.com/jquery-1.10.2.js"></script></script>
 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
</head>
<body>
</body>
</html>

In the above code, we load the CDN of Bootstrap and Jquery for running the data very well.
2) Let's create task_con.php in application/controller.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task_con CI_Controller
{
 
 function __construct()
 {
  parent::__construct();
  //Loading the model
  $this->load->model('pojo');
 }

 public function index(){
  /* Pagination of Data */
  $this->load->library('pagination');

  $config = [
   'base_url' => base_url('task_con'),
   'uri_segment'  => 3,
   'per_page' => 10,
   'total_rows'=> $this->pojo->task_pagination(),
   'full_tag_open'  =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
  ];

  $this->pagination->initialize($config); 
  $data['result']=$this->pojo->task($config['per_page'], $this->uri->segment(3));
  
  $this->load->view('task.php', $data); 
  
 }

In the above controller, we have covered the following points and here is the explanation of each line code:

a) $this->load->model('pojo'); For loading the pojo model in which we are going to create the two methods one is task method and another one is task_pagination method.
b) $this->load->library('pagination'); Is used to load the pagination library without which it does not work thereby, we need to load the pagination library first.
c) After that we create the $config array in that we are going to pass all the syntax of Pagination library.
d) 'base_url' => base_url('task/index'), Pass here the path of the method.
e) 'uri_segment'       => 3, this is the number for the path of URL(domain-name/controller/method). So write the number where current method comes.
f) 'per_page'=> 10, how many records show on the page.
g) 'total_rows'        => $this->pojo->task_pagination(),  this is used to count the number of records available in the system based on that it divides the numbers.

Let's create task_pagination method in application/model/pojo

public function task_pagination(){
    /* This display all task of Employees from database*/
    $this->db->select('*');
    $this->db->from('task');
    $query=$this->db->get();
    return $result=$query->num_rows();
  }


In the above method, business logic get all record of task and is passed to the controller.

 return $result=$query->num_rows();  it returns the number of all rows to the pagination.

h) 'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',


All the above are for pagination link.

i) $this->pagination->initialize($config);  This code is to initialise the $config syntax.
j) $data['result']=$this->pojo->task($config['per_page'], $this->uri->segment(3));

This line is passes the ($config['per_page'], $this->uri->segment(3))  to the hodm method in pojo.php model.

Let's create the hodm method in application/model/pojo.php


public function task( $limit, $offset){
    $this->db->select('*');
    $this->db->from('task');
    $this->db->limit($limit, $offset);
    $query=$this->db->get();
    return $result=$query->result();
  }


public function task( $limit, $offset) $limit and $offset is passed for the pagination.


k) $this->load->view('task.php', $data) At last, fetch data are stored in $data array and $data passed to the task.php view file.

Here, is the code of View file(task.php)

<body>
<div class="col-md-12">
<input class="search" type="search" data-column="all">
<div class="table-responsive" id="task">
<table class="tablesorter">
<thead>
<tr class="">
<th>Time</th>
<th>Task</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $row) { ?>
<tr>
<td>
<td><?php echo $row->duration;?></td>
<td><?php echo $row->task;?></td>
<td><?php echo $row->status;?></td>
</tr>
<?php } ?>
</tbody>
</table> 
<?= $this->pagination->create_links(); ?>
</div>     

</div>
</body>

 $this->pagination->create_links() pass this tag after table tag for creating pagination.

That's it. If you have any queries, feel free to write in comments down below!!

Stay tuned for some more coding!!

Thank you.




No comments:

Post a Comment