Back to Top

Getting Started with CodeIgniter

Getting Started with CodeIgniter

Today I am going to cover small tutorial about CodeIgniter.CodeIgniter is a PHP framework for web application.Basically, CodeIgniter follows the most well-known Model, view and controllers (MVC) architecture. With CodeIgniter, you can write your code from the scratch and maintain some code design pattern which can reduce your development time and increase performance.

CodeIgniter is a simple, lightweight and robust framework which provide helpers, libraries and hook concepts.


Let’s Start from Installations:

Step 1:

First of all download latest version of CodeIgniter.After that extract zip folder and add that folder into the root directory.That’s it. your CodeIgniter set up is done.

Now let’s take a quick view of CodeIgniter folder structure.

Root Structure

Root directory of CodeIgniter mainly contains System, Application folder. System folder contains core code of code-igniter. We never work on system folders and its only for core files.

The main folder for us is Application in which we write code and its contain config, controllers, Models, views, helpers, hooks and libraries.see in below screen.


Now, we will dive in four directories which are mainly useful for basic coding in Codeigniter.

  1. Config: Config contains configuration settings and include some core libraries in which we need to change database configuration as per application.
  2. Controllers: Directory contains controllers.Its a intermediator between Models and Views.
  3. Models: Its contain all models of our application which can have a database information.Model class includes insert,update and retrieve queries.
  4. Views: Its for user view.Its contain template files of web page. Mainly view is for storing your html code and displaying it to the user

Step 2:

Next we will configure database details in database.php file which in application/config folder.
You need to change below code.

$db['default']['hostname'] = 'localhost'; // Replace with desired Host name
$db['default']['username'] = ''; // Replace with desired database username
$db['default']['password'] = ''; // Replace with desired database password
$db['default']['database'] = ''; // Replace with desired database username
$db['default']['dbprefix'] = '';// Replace with desired database prefix.

Step 3:

After configuring the database, run your application with URL http://localhost/ci/ and if you get below the screen, your application installed successfully.

Now it’s time to create simple Application with CodeIgniter.

A Simple Demo Application

We will create simple demo application which display category from the database table.

Step 1:

Let’s Create Category table and insert some categories over there.

CREATE TABLE category (
			id int(11) NOT NULL auto_increment,
			cat_name varchar(255) NOT NULL,
			PRIMARY KEY (id) )

Step 2:

After that create one new file “demo.php” in controllers folder and write below code inside demo.php

class Demo extends CI_Controller
	function __construct()

	function index($page = 0)
		$data['username'] = 'Bhumi'; // To Display Basic Info without Database
		$data['company'] = 'CreativeDev'; // To Display Basic Info without Database
		$this->load->view('v_demo', $data); // To load view file.
		$this->load->model('m_demo'); // To load model file.
		$result = $this->m_demo->getRows($page); // To get resultset of query
		echo "";
		foreach($result as $item)
			echo "';

		echo "
" . $item['cat_name'] . '
"; } }

First we create class Demo which is controller class and extends core controller CI_Controller.Inside controller class, we have called __construct and default function index.

Step 3:

Create files m_demo.php in models and add below code over there

class M_demo extends CI_Model
	function getRows($page)
		$query = $this->db->query("select * from category");
		return $query->result_array();

Step 4:

Create files v_demo.php in views and add below code overthere

echo 'Welcome '.$username.' from '.$company;

Now type in the controller name in the web browser’s address bar as follows:

Say if we are running it in the local PC under localhost:


And Your code will give below OUTPUT



Welcome Bhumi from CreativeDev

Also Read:
Image Upload in CodeIgniter
Codeigniter _remap function
Prepping Functions in CodeIgniter

That’s it.Hopefully, this post will helpful to get the basic idea about CodeIgniter. If you have any problem configuring CodeIgniter, Let me know via comments. Don’t forget to share this tutorial with others on Facebook & Google Plus.

Comments (5)

  1. […] More: Code Igniter Tutorial | Creative Dev […]

  2. it’s working, but the “echo ” in the controller seems strange, should be in the view?

  3. jafor iqbal says:

    Nice Tutorial
    Thanks brother

  4. Please mention the version of codeigniter in this of the problems with frameworks is with evey new version there are big changes.

  5. Since the admin of this site is working, no doubt very rapidly it will be famous,
    due to its quality contents.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How do I add items in Top navigation bar?

Posted on 5 years ago


How to Create ShortCode in WordPress

Posted on 7 years ago


Problem with German special character

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago