Codeigniter CRUD is a most requested tutorial for Learn CodeIgniter course. It is an overview to CRUD operations. CRUD stands for Create Read Update and Delete. We’ll see how easily we can develop a CRUD using Codeigniter’s active record queries.
We will Divide this tutorial into 5 parts:
- Create Database Table & respective model
- Create Operation
- Read Operation
- Update Operation
- Delete Operation
Prerequisites for Codeigniter CRUD Tutorial:
- Codeigniter configured
- Knowledge of MVC
- Using Codeigniter’s Database Library
If you don’t know any of above, I highly recommend you to please refer to above tutorials first. Then follow CodeIgniter CRUD tutorial.
Let’s Create Codeigniter CRUD
Overview: We will create a simple module of post table’s 4 CRUD operations. Which is create, read, update & delete using Codeigniter’s Active Record class.
Step 1: Create a database in mysql and add table named post by executing following query.
1 2 3 4 5 6 7 8 |
CREATE TABLE `post` ( `id` int(11) NOT NULL AUTO_INCREMENT, `post_title` varchar(200) NOT NULL, `post_content` text NOT NULL, `post_status` enum('publish','draft') NOT NULL, `post_date` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; |
Step 2: Set codeigniter database configuration file to point to your database. In my case database name is ci and it is in localhost.
1 2 3 4 5 6 |
$db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = 'root'; $db['default']['database'] = 'ci'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; |
Step 3: Create a model file inside application/models directory and name it postmodel.php. write following code for model functionalities.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Postmodel extends CI_Model { var $table = "post"; function __construct() { parent::__construct(); } function getAll() { $q = $this->db->get($this->table); if($q->num_rows() > 0) { return $q->result(); } return array(); } function add($data) { $this->db->insert($this->table,$data); } function update($data,$id) { $this->db->where("id",$id); $this->db->update($this->table,$data); } function delete($id) { $this->db->where("id",$id); $this->db->delete($this->table); } } |
Here we created a class with exactly same name as our model filename with first letter capital. In our case its Postmodel class which extends CI_Model base class of Codeigniter. Next we created constructor method and call parent class’s constructor. If you like you can remove it but i will put it as is in case we want to put anything in constructor. We also created a instance variable $table which stores table name for model. So we don’t have to change everywhere incase we change table name. Then we created a instance method getAll() which fetches all data of post table from database using active record. Here we return blank array incase post table is empty.
Second instance method is add() which has one argument. Which is an array of data needs to be inserted. add() function has only one line of code which do the insertion part for post table using passed data in argument.
Third instance method is update() which has 2 arguments. First is an array of data needs to be updated. Second is id for which data needs to be updated. Here we used database class’s where method to specify id for which data needs to be updated.
Forth and final instance method is delete() which has only one argument. Which is id of row that needs to be deleted. similar to update we have 2 line code for delete.
So we have our database table and model ready for Codeigniter CRUD. So let’s move forward to Create operation for Codeigniter CRUD. You can download Full source code till Codeigniter CRUD: Day 1 Create Model from below link.
Please like our Facebook Fan page for instant updates on Tutorials.
Pingback: Codeigniter CRUD: Day 2 Create Operation » CodeRiddles
Pingback: Codeigniter CRUD: Day 5 Delete Operation » CodeRiddles