Home » PHP7 November 2016

CodeIgniter: Add, Edit, Delete, View with Login & Register – MVC CRUD Application

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

This article is a continuation of my past article Simple CodeIgniter CRUD Application tutorial. The tutorial has add, edit, delete, and view feature but it didn’t contain login and registration feature.

– This article/tutorial uses session for login and registration of users.
– You have to register and then login in order to add news/article.
– You can view/edit/delete only those articles that are added by you.

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

==========================================================

Here’s the full code of controller, model, and views files. In this example, we will be having two controller class. One will be for User and the other will be for News. Similarly, there will be two model classes (User_model and News_model).

Let’s first create User controller and model class for user login and registration.

User Controller

(application/controllers/User.php)

Controller 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 login() function is used for logging users, register() function is used to register users in the application.

User Model

(application/models/User_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_user() function fetches/selects users by $id. get_user_login() function fetches user by checking username and password. set_user() function either edit or add user. delete_user() function deletes any particular user.

User Registration View

(application/views/user/register.php)

User Login View

(application/views/user/login.php)

News Controller

(application/controllers/News.php)

In the below Controller class 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.

While creating and editing news items, first of all, we check if the user is logged in or not. If not logged in, the user is redirected to login page.

Similarly, we also check if the logged in user is actually trying to edit/delete his/her own article (article added by him/her) or trying to edit/delete others’ article. If trying to edit others’ article then the user is redirected to news index page.

Here’s the full source code for News Controller.

News Model

(application/models/News_model.php)

In the below Model class, get_news() function fetches/selects news items by $slug name and user id. 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. User email is displayed if the user is logged in.

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. The Edit and Delete link are only displayed for logged in users.

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 news application as http://your-website.com/news. User login page will be found at http://your-website.com/user/login & user registration page will be at http://your-website.com/user/register.

Here is an example page of logged in user with some news added:

Simple CRUD MVC PHP CodeIgniter

Download Source Code from GitHub

Hope this helps. Thanks.

PHP

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.