How to Develop Basic CRUD Operations in CodeIgniter

Today I am going to demonstrate the basic installation of CodeIgniter, a PHP Framework, and a simple example of building a CRUD (Create, Read/Retrieve, Update and Delete) with it.

Pre-requisites

It’s assumed that you have an intermediate level of understanding of the following:

PHP, MySQL, OOP, MVC

Installation

Download

To work with CodeIgniter, we have to download its latest version (2.1.4 at the time of this writing) from its download link at http://ellislab.com/codeigniter

WAMP Server Environment

An environment with at least Apache and PHP is needed to work with CodeIgniter. For a Windows platform, WAMP server stack is readily available and its .exe can set up the environment in a few seconds. WAMP server setup can be downloaded from http://www.wampserver.com/en/.

Setup

Once the WAMP server is installed, please unzip the downloaded CodeIgniter file and rename the folder to “cisample,” which will contain the folders for application, system and user_guide, along with a few other files.

Now place this cisample folder inside C:\WAMP\www folder. It’s assumed that the WAMP is installed in its default location. If it is not, then please re-adjust the path accordingly.

Configurations

In this section, we will configure settings for the database to help build this CRUD sample quickly but with ease of use, reusability and security features. We will also configure some other settings to benefit from the core libraries of this framework.

The first file to setup the configurations is C:\WAMP\www\cisample\application\config\config.php.

Set up the base URL to http://localhost/cisample.

Remove “index.php” from this line’s contents:

like this:

This will make the URLs clean.

What are clean URLs?

To understand this we look at two different examples of URLs:

Normal URL:  http://www.example.com/page.php?id=45 

Clean URL:  http://www.example.com/page/id/45

The Normal URL has a ‘?’ (Query string character) in the URL which is neither user nor search engine friendly. Whereas the Clean URL looks more clean and it is not only user but also search engine friendly. The other benefit of clean URLs is that if the resource providing the functionality for this URL changes, it’s not necessary to change the URL.

For database configuration, first of all create a database in the MySQL database server. The name of the database will be “cisample_db” (without quotation marks). The database hostname, username, password and database are the necessary parameters required to set up the connection between CodeIgniter and the database server. All of the database related settings can be set up inside C:\WAMP\www\cisample\application\config\database.php.

There are a couple of settings needed in C:\WAMP\www\cisample\application\config\autoload.php.

To load the database library, change $autoload[‘libraries’] = array();  to $autoload[‘libraries’] = array(‘database’).

To load the URL and form helpers, change $autoload[‘helper’] = array();  to $autoload[‘herper’] = array(‘url’,’form’).

Auto loading of libraries and helpers makes life easier as one doesn’t have to load the libraries and helpers each and every time when they are needed. They are automatically loaded using the above configurations in the autoload.php file.

Example’s Introduction and Database Table Structure

This example will demonstrate a simple to-do List CRUD. It will also use a simple three column table to hold the to-do items. The name of the columns will be id, title, and detail.

It’s time to dive straight into the code.

1. Start the WAMP environment.
2. Create a table in the database with the following query:

3. Create a controller file “application/controllers/todos.php” for to-dos with the following code:

 

 

b) create.php

Create a To-do item

c) edit.php

Create a To-do item

d) view.php

 

 
e) success.php

6. Insert the following lines in your application/config/routes.php for todos just in case you want the fancy/different URLs from your application resources:

7. Make the following changes in your application/config/config.php for todos:

8. Set the following parameters in your application/config/database.php depending upon your database server settings, mine are set to defaults so:

9. Create a .htaccess file in the cisample directory inside your webroot directory with the following code for URLs like http://localhost/cisample/todos:

10. Now check your Apache web server configuration to make sure that the mod_rewrite module is loaded.

11. Create each of the following files under the application/views/templates directory with its respective code for template:

a) header.php

CodeIgniter Sample Site

b) footer.php

12. Load your todos list with http://localhost/cisample/todos.

Making a CRUD using CodeIgniter is both easier and more fun than it seems. CRUD is the first step in learning any new technology where databases are involved. You can move from here to explore more areas and become more comfortable with CodeIgniter. You’ll find those areas of CodeIgniter as easy as this tutorial. Happy CodeIgniting!

The following two tabs change content below.

Khurram K.

PHP Team Lead at Allshore Virtual Staffing
We are a collaborative company at Allshore Virtual Staffing, and encourage our employees to contribute content such as this.Allshore is a remote staffing agency helping startups in the U.S. hire remote software engineers to work as full-time employees. Allshore provides all technical and managerial support via our client services and technology mentorship teams based in Norman, Okla. Contact Allshore today to schedule a free consultation call with a remote staffing specialist or request a free trial of our services.

Latest posts by Khurram K. (see all)

  • How to Develop Basic CRUD Operations in CodeIgniter - Jun

10 Comments

  1. Hakeem-Reply
    Jun at 1:39 AM

    Nice article Sir!
    Carry on Good Job.

  2. Ramesh-Reply
    Jan at 12:15 PM

    Nice article, Try http://letsgen.com, where you can generate CRUD operations easily.

  3. sujit-Reply
    Jul at 4:00 PM

    nice codding for seperate parts but still confusing for begginers…

  4. Collins-Reply
    Jul at 4:06 PM

    Beautiful tutorial. Simple and perfect.

  5. Collins-Reply
    Jul at 8:31 AM

    Implemented the entire structure. everything worked for me. except the edit. Edit deosnt work. need your help pls

  6. Manish-Reply
    Oct at 10:55 PM

    Thanks for Nice Post,you can also take reference from http://www.tutsway.com/codeignitercurd.php.

  7. Nov at 3:11 PM

    Simple and clear tutorial. I copied your source codes, and worked no problem in my comp. Thanks.

  8. Apr at 12:02 AM

    We can create auto dynamic crud in codeigniter and other frameworks using http://crudgenerator.in

  9. Oct at 7:39 PM

    Thanks for sharing crud concept in very easy way. I m new in codeigniter php. It helps me a lot. I was used it in my website wiki-mobiles

  10. Jul at 12:27 PM

    nice thanks alot

Leave A Comment