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.
1 2 3 |
$autoload['libraries'] = array('database','session'); |
Step 2. Open application/config/config.php file. set encryption key for session to any word you want like below.
1 2 3 |
$config['encryption_key'] = 'coderiddles'; |
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.
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 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Post extends CI_Controller { function __construct() { parent::__construct(); $this->load->model("postmodel","post"); } function create() { if(@$_POST['create_post']) { $data = $_POST['post']; $data['post_date'] = date('Y-m-d H:i:s'); $this->post->add($data); $this->session->set_flashdata('message',"Post created successfully"); redirect("post/create"); } $this->load->view("post/create"); } } |
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.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<style type="text/css"> #container { width:900px; background-color: #d8d8d8; margin: auto; padding:20px; font-size:14px; font-family:tahoma; } label { vertical-align: top; padding:5px; } input,textarea { padding:5px; } .success { color:green; } </style> <div id="container"> <h1>Create New Post</h1> <?php if($msg = $this->session->flashdata("message")): ?> <p class="success"> <?=$msg?> </p> <?php endif; ?> <form action="" method="post"> <p> <label for="post_title">Post Title:</label> <input type="text" name="post[post_title]" id="post_title"/> </p> <p> <label for="post_content">Post Content:</label> <textarea name="post[post_content]" id="post_content" rows="5" cols="40"></textarea> </p> <p> <label for="post_status">Status:</label> <select name="post[post_status]"> <option value="publish">Publish</option> <option value="draft">Draft</option> </select> </p> <p> <input type="submit" name="create_post" value="Create Post" /> </p> </form> </div> |
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:

http://localhost:8888/ci/index.php/post/create
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.
Please like our Facebook Fan page for instant updates on Tutorials.
Pingback: Codeigniter CRUD: Day 1 Create Model » CodeRiddles
Pingback: Codeigniter CRUD: Day 3 Read Operation » CodeRiddles
Pingback: Codeigniter CRUD: Day 2 Create Operation - Code...