How to create CRUD in Codeigniter

I have already covered login form and registration form in my previous posts. In this tutorial I am going to cover how to create CRUD in codeigniter. I am using previous example and code for this post. If you want to look my previous posts on codeigniter then visit below links.

I am going to explain create, read, update and delete data from the users table. This post is going to be bit long. Now let’s start.


Create CRUD in codeigniter:

Create Database:

Create Users Table:

Controllers:

First I will create controller file as Dashboard.php. Remember Controller file must be start with capital letter and class name should match with controller file name.

Dashboard.php

In the construct function I have loaded the user model by calling $this->load->model('user_model') and form validation library. I will discuss about user model later in this post. In index function I got all users in $data[‘getUsers’] by calling getAllUsers() function of user_model. After that I loaded dashboard view and $data array to the dashboard view. addUser(), editUser() and deleteUser() will use later for CRUD.

Views:

Dashboard.php

First of all I use not empty condition on $getUsers array that we passed in controller. Then I print each row of $getUsers by using foreach loop. I have also created Add User, View, Edit, And Delete button to add, edit delete user respectively. After above writing above code my code will look like this.

how to create crud in codeigniter

Create Operation in Codeingiter CRUD:

Create Add User View:

Now I setup dashboard page successful now it’s time create add user code. First I will start with the view and create add user form in views/add_user.php

In the above code validation_errors() is used to print validation errors and $this->session->flashdata('successMsg') is used to print success message. $errorMsg will also print error message if record will not save. After that there is add user form with 5 fields.

add crud codeigniter

Create Add User Controller Function:

Read Operation in Codeingiter CRUD:

Create Read View:

Create Operation is done in CRUD now I am creating Read operation. Now I am going to create read view file view-info.php in views folder.

view info crud codeigniter

Create View Controller Function:

Update Operation in Codeingiter Crud:

Create Update View:

Read user operation is done now it’s time to create update operation. First I have to create edit user form and show respective data in input fields for editing.

edit crud codeigniter

Create Update Controller Function:


Delete Operation in Codeingiter Crud:

So I have wrote Create, Read and Update operations in CRUD. Now it’s time to create last operation in CRUD that is Delete. I don’t need to create any view on delete operation. I just need to delete a record on clicking delete button in dashboard and show successful message if record would delete successfully.

Create Delete Controller Function:

Models:

User_model.php

User_model.php file has necessary functions which will communicate to database.

 

Leave a Reply

Your email address will not be published. Required fields are marked *