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.

Installations

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.

application

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) )
	                ENGINE=InnoDB;

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()
	{
		parent::__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 "
CATEGORY
" . $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)
	{
		$this->load->database();
		$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:

http://localhost/ci/index.php/demo

And Your code will give below OUTPUT

OUTPUT:

CATEGORY
Music
Dance

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 tutorials..one 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 to use SQLite Database in PhoneGap?

Posted on 5 years ago

Bhumi

Learn about the Docker and how it works

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 4 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 4 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi