Codeigniter tutorial

Codeigniter Tutorial To Create And Load Model For Beginners

Views: 545

Codeigniter Tutorial To Create And Load Model For Beginners

This codeigniter tutorial is a basic introduction to codeigniter models for beginners, this basic codeigniter model tutorial will demonstrate how to load codeigniter model  process request and display results, for this we need to have knowledge of what is a codeigniter model and how it is used.

What is codeigniter model for beginners

What Is Codeigniter Model

As we know codeigniter is a MVC (Model View Controller) framework, we learned about views and controllers, but what exactly is model in codeigniter? A model is the brain of your application in any MVC framework, model connects your application with all data sources like database, model holds every logic of your application and model processes your application logic before it is displayed. In short model is the brain of your web application, models can make your code reusable and you can call a model any time you need, so it makes easy to create an application and decreases time spent in coding if you are building a web application you should and must know about models and should know how to use them properly.

How to create a model in codeigniter for beginners

How to create a model in codeigniter

Creating a model in codeigniter is very easy. But there are some rules i.e. name class of model should always start with uppercase and filename of model should be same as class name of model. Now let us create a model for our home controller and access it. In this codeigniter tutorial we will learn how to create and use a model

Open the directory models inside your application directory of codeigniter, create a new file with .php extension and name it Home_model.php, now open Home_model.php with text editor, add the following lines to your model:


class Home_model extends CI_Model{

public function __construct(){

parent :: __construct();


function home(){




We have created a new class named Home_model by extending class CI_Model and added a function home to it where we will write all our codes.

Now append home function data as an array, we will join both the parameters passed in the array with help of our model and display on our view. For help check note below.

Inside the home function we will add the following lines to join name and last name and return result back.

$full_name=$data[‘name’]. ‘ ‘ .$data[‘last_name’];

return $full_name;

Note: We will append the function with $data array i.e. function home($data=array()).

Loading model in codeigniter controller

Loading model in codeigniter controller:

Whenever you need you can load any model in any controller it offers you flexibility to reuse your models and offers you a less coding environment. You can load models in a controller using the following .php parameter.


So we will load the model in our home controller by using the following parameter:


Add the above line inside the index function of home controller above every content of the index function as given below.

public function index()








Now we will pass the values to our model and retrieve results. So add these lines after model is loaded in index function






$data=array(); -> Here we declared $data as an array.

$data[‘name’]=’Bill’; -> here we set ‘Bill’ value for $data[‘last_name’].

$data[‘last_name’]=’Gates’; -> Here we set ‘Gates’ value for $data[‘full_name’]

$data[‘full_name’]=$this->home_model->home($data); -> Here we are passing variable $data to home function of Home_model model and storing the result in $data[‘full_name’].


Now we will pass the returned value of Home_model to our home view and display it we will need to pass data array to our view, so modify the line where we are loading the view home.php as followed.

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

In the above syntax we are passing variable $data to our view home so we can access the values of $data in our view file. Now open your views directory and open home.php with your text editor and echo the values we passed. At this moment you should know that in codeigniter whenever we pass a array, the array pointers turn into variables. So you can directly echo the variables accessing $data array will through an undefined variable error. So use php echo to print the values like:

<p>My first name is <?php echo $name; ?><br>Last name is <?php echo $last_name; ?><br>And full name is <?php echo $full_name;?></p>

See snapshot of output:

Codeigniter Tutorial To Create And Load Model and For Beginners Output

If you need download the sample files of Model View and Controller


If you have successfully completed Codeigniter Tutorial To Create And Load Model and For Beginners then we will move to next : Codeigniter tutorial to create multiple views for beginners

Comments: 3

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