Step-by-Step Guide to the Effective Use of CodeIgniter

Step-by-Step Guide to the Effective Use of CodeIgniter

DebugMe is saying hello to CodeIgniter!

Undoubtedly, there are tens of thousands of people to know a lot about CodeIgniter. But, let’s keep this tutorial simple and elementary, so everyone can apprehend it and start working with the framework without having to take any professional course.

Step-by-Step Guide to Effective Use of CodeIgniter

 

Introduction to CodeIgniter

CodeIgniter is a PHP web application framework that enables programmers to build web applications faster. It is shipped with many helpful code libraries and helpers to simplify complex code operations such as email, form validation, image manipulation, file uploading, sessions, multi-lingual apps and creating APIs for your application. Based on a modular design, CodeIgniter can implement specific libraries adding to the speed of the framework.

 

Why CodeIgniter?

why choose codeigniter

A common question! There are various reasons that make CodeIgniter a premium web application development platform among PHP developers.

First it is a very light and well performing framework. It is really good for beginners because of the small learning curve: one can easily interpret and build its application using CodeIgniter. Secondly it is also apt for building large and complex web applications:

  • Runs on both PHP 5 (and above) and 7
  • Emphasizes on application security and offers easy caching operations
  • It supports all the major databases including MySQL (4.1+), MS SQL, MySQLi, Postgres, Oracle, and ODBC.
  • Based on ‘use when needed’ meaning most libraries are loaded only when they are actually used. This helps in the management of resource availability.

So let’s begin our actual learning. We’ve done our best to cut down the learning curve to make it easy to understand for beginners also.

Recommended article: Bug Fixing and Handling Bug Reports the Right Way 

To begin with…

We need to first create a Model, a Controller, and a View along with the database to hold the information. To create the database, we will need the following:

  • a table that contains 3 columns – id (set as primary and auto incremented), title, and body. While you can add more columns as per your need, for now, this is enough.
  • a couple of rows in the table with some arbitrary items.

Apart from creating the database we’ll also need to configure CodeIgniter to use this database.

 codeigniter

The Model

Once you’re done with creating the database and setting up CodeIgniter, head towards creating a Model class, which will query it for information. For this, look for the “application/models” folder of your CodeIgniter installation and create a new php file with the name news1_model.php. You can choose any name for your file.

Next, add a php opening tag at the top of the new file. Create the Model class by extending the default CI one. Make sure you begin the class names with capital letters:

class News1_model extends CI_Model

 {

 

}

Classes are like the home for functions. So for every class, we create a function(s). Here we are creating a function to query the database. CodeIgniter makes it easy to work with the database using the Active Record pattern. Simply, load the database class in the constructor function.

To load it, use the following function into the class:

public function __construct()   {

  $this->load->database();

}

With this code, all the methods within this class will be able to make use of the database functions. Now, to make your method read the information from your database table, paste the code:

public function get_news1($id) {

  if($id != FALSE) {

    $query = $this->db->get_where(‘news1’, array(‘id’ => $id));

    return $query->row_array();

  }

  else {

    return FALSE;

  }

}

This piece of code will simply check that an ID is allotted to it before accessing from the table. If no ID is there, the function will return FALSE.

Save the file and exit.

Recommended article: Front-End Libraries and Frameworks – Which to Choose?

The Controller

Now, we’ll work on to handle the request from any client that demands this information. For this, go to the “application/controllers” folder and create a new php file called news1.php. Remember, this filename must correspond to the Classname you are about to give. Again, create an opening php tag and the Controller class with extension:

class News extends CI_Controller {

 

}

 

All the complex retrievals occur in the Model class. So, here we will load it and send it to the ID argument of the information we want to display. For this, let’s create a function called show() to handle this:

public function show($id) {

    $this->load->model(‘news1_model’);

    $news1 = $this->news1_model->get_news1($id);

    $data[‘title’] = $news1[‘title’];

    $data[‘body’] = $news1[‘body’];

    $this->load->view(‘news1_article’, $data);

}

Let us understand the code thoroughly:

The first line loads the news1_model, while the second one uses it to store the query result in the $news variable. The next two line correspond to storing the title and body of the retrieved news in the $data array. The last line loads the View we want to display them as and passes the information along.

Now, we will build the View File where each key in the $data array will represent a variable to be printed out.

The View

Click the application/views folder and create a new php file, news1_article.php. Keep the name same as the one you loaded in the Controller. In this file, paste the following code:

<?php print $title; ?>

<?php print $body; ?>

Here, the $title and the $body variables are being passed from the Controller to the View using the $data array. If you want to add any form of markup to change the display of the page, you can use the H1 tag for the title and a paragraph for the body.

Now, save the file. Point your browser to the Controller you created earlier:

http://example.com/news1/show/1

When the browser makes this request, CodeIgniter will look for a Controller named news1, call a method within it named show(), and pass it the parameter 1. Further, this method will load the news1_model to query the database and returns the information. The Controller will then pass this piece of news1 to the View for display.

 

With this, you are done with building your first basic but dynamic application. We hope this guide will help you understand CodeIgniter and its working ways.

 

About the author – Kerin Miller is an experienced CodeIgniter developer and a part time blogger. She works for a custom CodeIgniter development company – Stellen Infotech. When Kerin is not working, she likes to share insightful posts, covering web and mobile application development industry.

 

The makers of DebugMe hope that you like this post and would love it if you follow us on Twitter. We are tweeting about web design, UI/UX  and development related topics multiple times a day. DebugMe is an issue tracking, project management and screenshot tool for every website project. Get visual feedback right away and solve front-end problems faster. Try it now for free.

SHARE ON:Share on LinkedInTweet about this on TwitterShare on Facebook

2 thoughts on “Step-by-Step Guide to the Effective Use of CodeIgniter

  1. Codeigntier provide an underlying framework architecture that helps you get to creating your application without having to worry about the very basics when it comes to doing tasks like validating user input, etc. IT is also light weight and easy to learn and use. But when you need to build a large scale and more robust web application then you might like to go for Laravel.

Leave a Reply

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