CRUD – Codeigniter, Datatables, Ajax

Codeigniter is a PHP framework containing libraries, helpers, plugin-in, and other resources. It will make the codes in PHP simple, quick, and user-friendly.  It is a lightweight MVC programming technique to keep the business logic separate from the display logic. MVC stands for Model View Controller which is a software development method splitting the data layer (Model), the business logic (Controller), and the layout of the application (View). With the help of this framework, we’ll be building a simple CRUD with DataTables and Ajax.

Table of Content

  1. Introduction
  2. Configure Config File
  3. Configure Database
  4. Database Connection
  5. CRUD Demonstration
  6. Source Code

1. Introduction

CRUD stands for Create, Retrieve, Update, Delete to interact with the database table’s data.  As a web developer, CRUD is a usual work in your career, and with the integration of the datatables plugin, it will make the table more interactive, feasible, and Better UI. You can download the source code and database SQL query at the end of this tutorial. You will have to make some changes in the source code to run the program

2. Configure Config file

In this part, Go to the config.php file i.e. application/config/config.php and you must change the base URL root to work the code properly without any errors. You will have to modify the base URL value in the config file to help the CodeIgniter to guess the protocol and path of your installation.


<?php
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
| http://localhost/crud_datatables_codeigniter
*/
$config['base_url'] = 'http://localhost:9080/crud_datatables_codeigniter';

?>

3. Configure the Database

Database Name: crud_datatables_codeigniter
Table Name: members
Table Column: id, fname, lname, age, contact, address

To create the database for this tutorial, either you can download the SQL file which is provided at the end of this tutorial or copy and paste the following SQL command into your MYSQL Database.


CREATE DATABASE `crud_datatables_codeigniter`;

CREATE TABLE `crud_datatables_codeigniter`.`members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(255) NOT NULL,
  `lname` varchar(255) NOT NULL,
  `age` varchar(255) NOT NULL,
  `contact` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

4. Database Connection

In database.php file i.e. [application/config/database.php], contains a database connection. If you have a different database name then change the database name in the database.php file highlighted below.


$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'crud_datatables_codeigniter',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

6. Source Code

Download