Codeigniter 3 Datatables Ajax Example From Scratch

By Hardik Savani | January 28, 2018 | | 14027 Viewer | Category : PHP Bootstrap jQuery MySql Codeigniter Ajax

Datatables is one of the best libraries for display data in tabular format and easily ajax search, sort, pagination etc. here I give you an example of CodeIgniter 3 with database ajax integration example. you can easily use jquery ajax datatables in your codeigniter project.

This post is going to look at how you can implement datatables plugin into your CodeIgniter application. We will be grabbing some data from a database and then using Datatables' plugin to display it, allowing for ajax searching, sorting and pagination feature.

I would like to give you very short and quick example, so you can get it quickly and easily. You have to just follow below step and get a full example like as below screenshot.


Step 1: Create items table

In this step we will create new new table "items" in database. You can use following SQL Query for create "items" table. So let's create using bellow sql query:

items table:


`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

`description` text COLLATE utf8_unicode_ci NOT NULL,



Step 2: Create Routes

Here, we will add new routes for list items. so open routes.php file and add code like as bellow:



defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';

$route['404_override'] = '';

$route['translate_uri_dashes'] = FALSE;

$route['item'] = "item/index";

$route['get_items'] = "item/get_items";

Step 3: Create Item Controller

now, we have to create "Item" controller with index() and get_items(). so create ImageUpload.php file in this path application/controllers/Item.php and put bellow code in this file:



defined('BASEPATH') OR exit('No direct script access allowed');

class Item extends CI_Controller {


* Get All Data from this method.


* @return Response


public function __construct() {





* Create from display on this method.


* @return Response


public function index()





* Create from display on this method.


* @return Response


public function get_items()


$draw = intval($this->input->get("draw"));

$start = intval($this->input->get("start"));

$length = intval($this->input->get("length"));

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

$data = [];

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

$data[] = array(






$result = array(

"draw" => $draw,

"recordsTotal" => $query->num_rows(),

"recordsFiltered" => $query->num_rows(),

"data" => $data


echo json_encode($result);




Step 4: Create View File

In this step we will create index.php view file . In this file we will write design of html table and include datatables. So let's update following file:


<!DOCTYPE html>



<title>Codeigniter 3 Datatables Ajax Example</title>

<link rel="stylesheet" href="" />

<script src=""></script>

<link rel="stylesheet" type="text/css" href=""/>

<script type="text/javascript" src=""></script>

<script type="text/javascript" src=""></script>



<div class="container">

<h2>Codeigniter 3 Datatables Ajax Example</h2>

<table id="item-list" class="table table-bordered table-striped table-hover">













<script type="text/javascript">

$(document).ready(function() {


"ajax": {

url : "/get_items",

type : 'GET'






So let's run and see.

I hop it can help you...

We are Recommending you: