Home » PHP23 May 2016

CodeIgniter: Simple Add, Edit, Delete, View – MVC CRUD Application

This article shows how to create a simple CRUD (Create, Read, Update, Delete) application with CodeIgniter. CodeIgniter (CI) is a PHP framework that helps building a full-fledged web application.

This article is a continuation of the basic tutorial present in the official CodeIgniter site. The tutorial had view and add data part. But, it didn’t contain the update and delete part. I have just added update and delete functionality in it.

Here is the step-by-step guide on creating a CRUD (Add/Edit/Delete/Update) application in CodeIgniter.

Create Database and Table

First of all, we create database and table. For this article example, we will be creating database named test. We will be creating a table named news inside the database test.

I will present below the full code of controller, model, and views files. We will be adding, updating, deleting news item to our database table. News item just contains title and text content.

Database Config Settings

After you create your database, you need to specify your database host, database name, database username & database password in a database config file in codeigniter. The file is located at application/config/database.php.

My database host is localhost, database name is test (as created by above sql query), database username is root and database password is root.

Here is the config settings for this example:

File: application/config/database.php

Controller

(application/controllers/News.php)

First of all, we need a Controller which processes the input request from user, communicates with Model and then loads appropriate Views file.

In the below Controller class, we have different functions (actions) for different tasks. Like, the index() function is used for listing all news items, create() function is used to add a new news item, edit() function is used to edit the news item, and delete() function is used to delete the news item.

Model

(application/models/News_model.php)

Model class communicates with the database. The main database logic is written here. This class is responsible for interacting with database for data select, insert, update and delete purposes.

In the below Model class, get_news() function fetches/selects news items by $slug name. get_news_by_id() function fetches news by it’s ID. set_news() function either edit or add news item. delete_news() function delets any particular news item.

Template Header

(application/views/templates/header.php)

This is a template file common for all pages. We call header in all pages.

Template Footer

(application/views/templates/footer.php)

This is a template file common for all pages. We call footer in all pages.

Index View

(application/views/news/index.php)

This view file is called by index() function of our Controller class. It lists out all the news item.

Detail View

(application/views/news/view.php)

This view file is called by view() function of our Controller class. It prints a particular news item.

Add View

(application/views/news/create.php)

This view file is called by create() function of our Controller class. It prints a form to add news item.

Edit View

(application/views/news/edit.php)

This view file is called by edit() function of our Controller class. It prints a form to edit news item.

Add Success View

(application/views/news/success.php)

This view file is called after a new news item is added to database. This file is called in create() function of our Controller class.

Config Routes

(application/config/routes.php)

Routing rules are defined here. Routes can either be specified using wildcards or Regular Expressions.

Now, you can access your application as http://your-website.com/news.

When you open your project in browser, you will get something like below image:

Simple CRUD MVC PHP CodeIgniter

Download Source Code from GitHub

Hope this helps. Thanks.

php magento mukesh chapagain

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Rahul Dubey

    I have created a table in mysql database using php, and also i can displayed that table on the web form, butnthe thing is that i want to do is, I have six field in my table ie. Sr_no which is auto increment, Process_no, Process Name, Ownership, Sheet Revision_no and Revision Date. Now i want to make a dynamic treeview type in my field Process_no for eg:, i have the value in that field M01, so when i’ll click on M01 then their shuld be a sub-list under it such as M01.1, M01.2,….and so on. and this thing i want in every column of that field.
    i had tried alot but fail to do it.
    if you hav any solution or any code for it then plss help mee, plsssss..
    i’m not a experience candidate, new at php and mysql.

  • nirav

    thank you boss

  • Jess Jaime

    Works perfect ! thanks

  • DerekJR321

    Extremely helpful. Thanks!

  • Squirrel

    You are awesome!!!