JQuery Datatable Server Side Processing Use Codeigniter and MySQL PHP.
By: Lovelesh Singh
Tags: MySQL, Codeigniter, Datatables, Jqu, PHP

How to get huge number of rows in jquery datatable from database using PHP, Codeigniter and MySQL ?

Let’s start for learn something new in jquery Datatable.

Normally we are used jquery datatable in our web projects, But we are faced loading issue when fetching large data from database. The pagination of jquery datatable is not work smoothly. For solve this problem we are used server side processing in jquery datatable. The server side datatable processing solve the loading issue when fetching millions of rows.

In this article we will learn how to fetch millions of rows from the database smoothly.

Now Start =>

Step 1 : We will use codeigniter 3 and jquery datatable CDN and bootstrap CDN.

Step 2 : Open the editor and open the project folder in the editor.

Step 3 : Create a database and configure it with your project in application/config/datatable.php

Step 4 : Now we will use the jQuery Datatable CDN in the view page. Paste the CSS link between the head tag of the page, and paste the JS link before the closing of html tag.

CDN download from : https://datatables.net/download/

Step 5: Now we will use the bootstrap CDN in our view page. Same as step 5 Paste the style link before the closing of the head tag and paste the javascript link before the closing of html tag.

CDN Download from: https://getbootstrap.com/docs/3.3/getting-started/

Bootstrap-theme.min.css use of this link is optional not compulsory.

Step 6: Will use the jquery in view page, link must be above the datatable link.

Step 7: Now create a html table in the view page with pagination and create static sample data.

Step 8: Call the datatable function.

$(document).ready(function() {

$('#example').DataTable();

} );

Step 10: Now create a controller and model for fetching the data from the database. Now i am using my sample datatable which has 1000 rows and 5 columns.

Step 11: Now implement the jquery AJAX datatable function for fetching of data from database using controller and model of codeigniter.

$(document).ready(function() {

tableLoad();

function tableLoad() {

var table = $('#example').DataTable(

{

"processing": true,

"serverSide": true,

"ajax": "<?=base_url('Welcome/getData')?>",

"order": [[2, 'desc']],

"columnDefs": [

{"targets": 0, "name": "id", 'searchable': true, 'orderable': true},

{"targets": 1, "name": "post_type", 'searchable': true, 'orderable': true},

{"targets": 2, "name": "caption", 'searchable': true, 'orderable': true},

{"targets": 3, "name": "img_1", 'searchable': true, 'orderable': true},

{"targets": 4, "name": "username", 'searchable': true, 'orderable': true},



]

});

}

} );

After using this code in the view page we need to set data in json format in controller method and the code set in controller method.

/**

* @method use to get data from get data method of welcome model

*/

public function getData() {

if(isset($_GET['search']['value'])){

$search = $_GET['search']['value'];

}

else{

$search = '';

}

if(isset($_GET['length'])){

$limit = $_GET['length'];

}

else{

$limit = 10;

}

if(isset($_GET['start'])){

$ofset = $_GET['start'];

}

else{

$ofset = 0;

}

$orderType = $_GET['order'][0]['dir'];

$nameOrder = $_GET['columns'][$_GET['order'][0]['column']]['name'];



$records = $this->Welcome_model->getData($limit , $search , $ofset , $nameOrder , $orderType);

$data = array();

$i=0+$ofset;

foreach ($records['data'] as $row)

{

if($row['status'] == 0){ $status = ' <span class="btn btn-danger btn-sm tgl_change_post danger" data-status="1" id="post_'.$row['user_id'].'" data-id="'.$row['user_id'].'">Inactive</span>';}

elseif($row['status'] == 1) { $status = '<span class="btn btn-success btn-sm tgl_change_post" data-status="0" data-id="'.$row['user_id'].'" id="post_'.$row['user_id'].'">Active</span>';}



// update_post_status('ci_ads' , $row['id']);

$data[]= array(

++$i,

$row['username'],

$row['first_name']. ' '. $row['last_name'],

$row['gender'],

$status,

);

}

$records['data']=$data;

echo json_encode($records);



}

In the controller we get the datatable variables in GET method like limit, order, search, offset with the values and now create a method in the model for fetching data from the database.

/**

* @method use for get data from data

*/

public function getData($limit , $search , $ofset , $nameorder , $orderType ) {



$query1 = $this->db->get('user_details');

$total = $query1->num_rows();





$query = $this->db->get('user_details');



$totalRecord = $query->result_array();

$filtered = $query->num_rows();



return array("recordsTotal"=>$filtered,"recordsFiltered"=>$total,'data' => $totalRecord);

}

Now we will get the result after these implements and the result is.

Step 12: Now we will work in our query for fetching the data according to need of the user like limit , search and order etc.

For Data in limit will use the limit in query :

public function getData($limit , $search , $ofset , $nameorder , $orderType ) {



$query1 = $this->db->get('user_details');

$total = $query1->num_rows();



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

$query = $this->db->get('user_details');



$totalRecord = $query->result_array();

$filtered = $query->num_rows();



return array("recordsTotal"=>$filtered,"recordsFiltered"=>$total,'data' => $totalRecord);

}

Now table look like:

For Search use like method in query:

public function getData($limit , $search , $ofset , $nameorder , $orderType ) {



$query1 = $this->db->get('user_details');

$total = $query1->num_rows();



if(!empty($search))

$this->db->like('username' , $search);



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

$query = $this->db->get('user_details');



$totalRecord = $query->result_array();

$filtered = $query->num_rows();



return array("recordsTotal"=>$filtered,"recordsFiltered"=>$total,'data' => $totalRecord);

}

Now Result Look like:

For order in ascending descending:

public function getData($limit , $search , $ofset , $ordername , $ordertype ) {



$query1 = $this->db->get('user_details');

$total = $query1->num_rows();



if(!empty($search))

$this->db->like('username' , $search);



$this->db->order_by($ordername , $ordertype);

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

$query = $this->db->get('user_details');



$totalRecord = $query->result_array();

$filtered = $query->num_rows();





return array("recordsTotal"=>$filtered,"recordsFiltered"=>$total,'data' => $totalRecord);

}

Now data look like:

I hope you may learn how to use DataTable Processing from database in Codeigniter. Please share your thoughts in the comment section below.

For Example code you can clone from git hub

Follow Me on github