This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

Create a Simple MVC Codeigniter CRUD Application

Updated on  18th September

9 Min Read
codeigniter crud
Reading Time: 9 minutes

Codeigniter is popular choice for projects that require database integration and interfacing. The main reason is the simple within-the-framework options for setting up and working with database connections. This means that Codeigniter is a great option for MVC CRUD applications. Before going into the details of this Codeigniter CRUD application, I will describe the basics of Create, Read, Update and Delete.

You Might Also Like: How to Install Codeigniter

Create, Read, Update, and Delete

All database based applications operate on a simple process that involves four simple operations – Create, Read (Retrieve in some cases), Update and Delete. All these operations are applied to the database and are essential for the operation of the application.

Since an application has users with varying privileges, it is important to note that not all users could carry out the four operations within the application. Similarly, the application process could also carry out these functions without any specific user input.

  • Create operation is used to create or add data to the database.
  • Read/Retrieve operation is used to read the data that is already saved in the database.
  • Update operation is used to save or update the data in the database.
  • Delete operation is used to remove data from the database.

Now that you have a basic understanding of CRUD, I will demonstrate how to implement these ideas in a Codeigniter CRUD application.

You Might Also Like: CRUD operation with PDO Database Connection

Create the Database and the Table

I will start by creating the database and a table that will act as the basis of this tutorial. For the purpose of this Codeigniter CRUD example, I have named the database as ‘crud’ and the table as ‘faqs’. Use the following SQL statements for creating the two:

Insert Values in the Database

Once the database and the table is up and running, the next step is adding value to the table faqs. Note that the fields in the table include After creating the database and table, I will insert values in the faqs table (as mapped by the fields id, title, slug and text). Use the following SQL query:

(1, ‘Do I have to pay separately for the infrastructure?’, ‘separately-for-the-infrastructure’, ‘No! This is the beauty of our WordPress Cloud Hosting Platform. Prices include infrastructure charges as well.’),
(2, ‘How secure is your platform?’, ‘secure-your-platform’, ‘We take the security of our managed WordPress hosting very seriously. We have deployed platform-level firewalls on all WordPress servers. Our experts have also used top-notch security hardening techniques to make sure that all servers stay protected.’),
(3, ‘How can I scale my WordPress hosting server?’, ‘WordPress-hosting-server’, ‘Through our WordPress Hosting Platform, you can vertically scale all the aspects of your WordPress cloud server. In some cases, cloning may be required.’);

Configure the Database Settings

The next step after setting up the database and the table is the configuration of database specific settings including the host, database name, user credentials. This is is generally done by adding these information to a Codeigniter database config file, located generally in the file application/config/database.php.

For the purpose of this tutorial, I have set the following values:

The complete database configuration are:

Next up, Controller

The next major step is the creation of the Controller that processes the user’s request, talks to the Model (the business logic component) and finally, loads the proper View.

You Might Also Like: Pass Data From Controller to View in CodeIgniter

The Controller is located in the file, ‘application/controllers/Faqs.php’. For the purpose of this tutorial, I have creates a controller with three major functions. The index() function lists all the FAQ items, the create() function adds new FAQ item, the edit() function is called to edit the already published FAQ items, and finally the delete() function removes the FAQ items from the database. Here is the complete code for the controller:

Build the Model

The Model contains the “business” logic of the MVC CRUD application. For the purpose of this tutorial, the Model of this application communicates with the database. As such, the Model contains the code that interacts with the database and performs the data select, insert, update and delete operations.

The Model developed for this tutorial has the several functions including: get_faqs() function that fetches the FAQ items (by $slug name), get_faqs_by_id() function fetches FAQ by ID number, set_faqs() function edits and adds new item into the database, and delete_faqs() function that deletes a particular item from the database.

Note that this model class has been created in the directory ‘application/models/Faqs_model.php’.

Template Header

The header of the template is common for all the pages. Note that this template is stored in application/views/templates/header.php. The full HTML code is as follows:

Footer Template

Similar to the header, the footer is common to all pages. The file is located in application/views/templates/footer.php. The full HTML code is as follows:

Create the Index View

This view file is used to display the list of all the FAQ items. The file is called by the index() function of the controller. The file is located at ‘application/views/ faqs /index.php’. The full code of the index file is as follows:

Detail View

This view file is called in by the controller’s view() function to display a specific FAQ item. The file is located in ‘application/views/news/view.php’. The full code of the view file is:

ADD View

This view is called by the create() function in the controller class, and it prints a form to add the FAQ item to the database. It is located in the file, application/views/ faqs /create.php. here is the complete code:

EDIT View

The edit() function of the controller called this view file and prints a form to edit the FAQ items. This file is found in application/views/ faqs /edit.php. The full code of the edit view file is:

Successfully Added View

This view is called by the create() function of the controller when a FAQ item has been successfully added to the database. The file is located in the directory application/views/faqs/success.php. This very simple view is as follows:

Config the Routes

Now that all the view are done, it is time to configure the routes in the route.php file. The good thing is that the these routes could be defined by using either (or both) regular expressions or through wildcards. This file is located in the file, application/config/routes.php. The code of the route is:

Conclusion

At this point, you have a simple yet complete MVC CRUD Application powered by Codeigniter. While a bit crude on the design end, the application can add, display and edit FAQ items without any issues. You can use this Codeigniter CRUD application as the basis of your own CMS project. To see the application in action, access the URL of the live application.

Share your opinion in the comment section. COMMENT NOW

Pardeep Kumar

Pardeep is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He love to work on Open source platform , Frameworks and working on new ideas. You can email him at pardeep.kumar@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.