Codeigniter CRUD: Day 2 Create Operation

Codeigniter | Controller | Crud | View

Codeigniter CRUD Day 2


This entry is part 2 of 6 in the series CodeIgniter CRUD

Codeigniter CRUD: Day 2 Create Operation: In Day 1 Create Model we created backend model which is responsible for all database related tasks. Now we have our database logic ready let’s move on to Create Operation of Codeigniter CRUD.

Before we start working on Controller Code. Let’s first set some configuration required for Codeigniter CRUD Tutorial.

Configuration for Codeigniter CRUD

Step 1. Open application/config/autoload.php file. Add database & session libraries to autoload['libraries'] array. Just like below.

Step 2. Open application/config/config.php file. set encryption key for session to any word you want like below.

So all configuration required for Codeigniter CRUD is done. Let’s move on to next step.

Create Controller for Codeigniter CRUD

Create a new file in application/controllers directory and set it’s name to post.php. Write following code in post.php controller.

As you can see I have created a class which extends codeigniter’s base controller class which is CI_Controller (For older CI version it will be Controller or may be something else check your version’s documentation). I set name of class to what my controller’s file name which in this case is Post. Next I have to create a constructor and load model which we will need in all operations of Codeigniter CRUD. To do that i write  $this->load->model("postmodel","post"); . Remember the second argument of load model method is instance name for model. So now we can access model functionality using  $this->post .

Next we created a function create. Here we check if request is post request by checking create_post post variable which we will set in our view in a moment. If it is a post request then we will collect post data and pass it to post model’s add method. and redirect to same page with flashdata of session library.

If it is not a post request then we simply load create view. So our next step is to create a view.

Create View for Codeigniter CRUD’s Create Operation

Create a directory inside application/views directory and name it post. Create a file in newly created post directory and name it create.php. Add following code in create.php file.

Remember: If you want you can load separate file for stylesheet.

In view file we simply check for flashdata and if we have flashdata then output it. next we created a form and place text, select controls for post creation. Simple HTML nothing difficult.

Now we have create operation ready to use. let’s open browser and load create method of post controller. Assuming your codeigniter is running on localhost:8888/ci/, write http://localhost:8888/ci/index.php/post/create  and you will get something like:

Codeigniter CRUD Create Operation


Fill in proper details and click on create post and you will get success message that we passed through flashdata from controller. So we have our Create operation of Codeigniter CRUD working perfectly. So let’s move forward to Read operation for Codeigniter CRUD.


Download Source Code Files

Please like our Facebook Fan page for instant updates on Tutorials.

Series Navigation<< Codeigniter CRUD: Day 1 Create ModelCodeigniter CRUD: Day 3 Read Operation >>

Bookmark Link using: bookmark at folkd