Codeigniter Tutorial

Course Banner


Codeigniter

About this tutorial

In this tutorial, I am going to show you how a sipple Insert, update and delete operation can be performed using CodeIgniter. This step by step tutorial will help you to understand the basics of how the MVC of this framework works.

Prerequisite :

  1. You must have knowledge of basic HTML and css.
  2. You must have knowledge of PHP and MySql.
  3. You must have knowledge of object-oriented PHP

Target audience :

  1. Any PHP web developer who wants to learn codeIgniter
  2. Anyone who has very basic knowledge of CodeIgniter and wants to know how CRUD operation works in CodeIgniter.

Project Setup

  1. Go to https://codeigniter.com/download and download codeigniter 3.0
  2. Unzip and paste it into your htdocs folder of xampp folder.
  3. Open into browser http://localhost/ci_crud

Setup BaseURL

First, let's set-up base URL of the project

Please, open config.php within codeigniterapplicationconfig. Set config like below:

Database Setup

We need to do database configuration also.

Please, open database.php within codeigniterapplicationconfig. Set config like below:

Preparing Database

We will learn about showing data from database in CodeIgniter. But, before that, we prepare a database for practice. This post create a database named “ci_test” and a table named “users”. We use phpMyAdmin for easy.

Open your phpmyadmin.
Enter database name “ci_test” in create new database field.
Click the Create button. Your database will be created.
Create new table by running this query in your sql window:

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`f_name` varchar(50) NOT NULL,
`l_name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
               PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

Insert some sample data to your users table by running this query in your sql window:

INSERT INTO `registration` (`id`, `f_name`,`l_name`, `email`) VALUES
(1, 'Umar',Rahman, 'umar@gmail.com')
INSERT INTO `registration` (`id`, `f_name`,`l_name`, `email`) VALUES
(1, 'Siran',Alam, 'siran@gmail.com.com')
INSERT INTO `registration` (`id`, `f_name`,`l_name`, `email`) VALUES
(1, 'Kisn',Kumr, 'kisan@gmail.com')

Showing the details

Once our database setup is donem, our next task is to display the records that we have just entered. SInce displaying data is one of the easiest steps in CRUD operation , that's why we are going to do this first.

First, build a model. Create a file named “registration_model.php” within codeigniter/application/models. Enter the following code:

<?php
class Registration_model extends CI_Model {
function __construct()
{

parent::__construct();

$this->load->database("ci_test");
}

public function get_all_users()
{
$query = $this->db->get('users');

return $query->result();
}
}
?>

After this we need to create view page. The view page will help us to display the data received from model via controller.


Ad Image