Thursday, 11 February 2016

How to create pagination in codigniter?

How to create pagination in CodeIgniter: First of all you must know what is pagination. Actually, pagination is the division of data into a number of pages.We divide the data into pages and each page can contain your own number of choice data to be displayed on each single page. Before the CodeIgniter pagination people have to manually create the things but now the CodeIgniter provide you lot of the things to do by just using the libraries of the framework. This is also the standard way where people work in the development field. Here we explain you the method how you can implement the pagination of CodeIgniter. We assume you know about the CodeIgniter controllers models and view that is MVC based framework, here is the controller where we set our logic to get the data from our database.

How to create pagination in codigniter

Controller

//pagination
First, load the pagination library by writing the code below it will load the functions that are going to be used after.

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

Configure the base URL that means the controller where you are displaying the data.

$config['base_url'] = "http://localhost/projects/helloworld/loginAdmin/";

Here we get the total number of results.

$total_records = $this->db->count_all('tablename');
$config['total_rows'] = $total_records;

Set the total number of results to show per page. Like here we set to display the 5 items per page on our website.

$config['per_page'] = 5;

Initialize the pagination.

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

Create the segments, segments are the number of pages that are written after the controller/function_name/segment_number understand the thing if now understand still tell us we will clear further in detail.

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

this statement creates the links of base URL and displays the number of results.

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

then send the data and execute the query.

$data['result'] = $this->crud_model->get_item($config['per_page'] ,$page);

load the view and send the query returned data.

$this->load->view('tableview', $data);

Model

In the model of CodeIgniter mod_pagination.php have the function which will get the database results that we need to show on each page for that we write a query.
public function get_item($page, $limit){
   $this->db->limit($page, $limit);
 $q = $this->db->get('testDb');     return $q->result();
}

View

In CodeIgniter, each and everything is being done like the pattern of MVC, model view and then controller so, here is the view. The page gets the variable from the controller and then we will display that in the pages.

<p class="text-center"><?php echo $pagination; ?></p>

How to create pagination in codigniter

This is the simple and easy way to do  or implement the pagination in the web application using CodeIgniter. People like to use the framework for their product development because of the fast and easy way to develop then compare to the custom language development. If you have any query in the pagination you can ask here the comment box. For further reading, you can view the CodeIgniter queries that will explain to you how you can write the database queries in an easy way using Codeigniter.
Codeigniter Queries or PHP Queries....!

This Post Was Last Updated On September 30, 2016, By Author: Mahira Khan.

0 comments:

Post a Comment

Item Reviewed: How to create pagination in codigniter? Rating: 5 Reviewed By: admin