C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

How to Pass Data from Model to Controller


As we learn in the architecture of CodeIgniter, we should convey our database data into View section. In previous article, we successfully retrieved some data from database. But we will not display those values on Model or Controller sections.



To reach a database value from Model to View, then that should be via Controller. In this lesson, let’s see how to send that database retrieved data into the Controller.

Mainly there are two ways to pass data from Model into Controller.

Sending Model variables into Controller

Method 1:

Using Model Variables

This method is just opposite of the passing a data from Controller into the Model that we learnt in a previous lesson. There we used Model variables and we could access the Model variables on Controller section. Like that, we assign database retrieved value into a new Model variable and controller can access this variable.

You will more idea by this example:

As we shown in previous lesson, we retrieve a name of a user from database his age is ‘25’. But instead of showing name on Model directly, we store its value on a model variable that we created now.

The variable name is ‘DBNAME’.

var $DBNAME = '';

Now we store the database value to this variable using the code 

$this->DBNAME = $row->user_name;

Now our Model will be like this: 


var $DBNAME = '';
public function getage()
{
  $this->load->database();
  $query = $this->db->query("Select user_name from table_user where user_age= 25");
  $row = $query->row();
  $this->DBNAME = $row->user_name;
}

Now variable DBNAME has the database retrieved value contains the a user name with age 25.

We already studied that, model variable can be accessible on Controller like we sent Controller data into the Model for inserting to database.

So let us access the variable DBNAME from our controller.

$this->ProjectModel->DBNAME;

Here I store the Model value DBNAME on another controller variable $data_from_db, and as a testing, I displays its value on controller.

Controller Code:


  public function getdata()
  {
   $this->load->Model('ProjectModel');
   $this->ProjectModel->getage();
   $data_from_db =  $this->ProjectModel->DBNAME;
   echo $data_from_db;
  }

Here the function getdata is the controller function that we created to retrieve data from database, and getage is the model function that we call from our controller.
Next line    $data_from_db =  $this->ProjectModel->DBNAME; gets database retrieved data from Model.

Data flow:

Sending CodeIgniter Model data into Controller

In above dataflow, codes for inserting data in both controller and Model are not included. See previous lesson to get those codes.
Here we send only one variable to the controller, like this, we can pass many variables to the controller, but we need to create enough variables on Model class.

Method 2:

Using Model Function Return Value

This method is more simple, and we don’t need to create Model variables.

Controller:

Here, while we calling Model function on Controller($this->ProjectModel->getage();), we assign this function to a variable.

For example,

$data_from_db = $this->ProjectModel->getage();

Model:

In model section, we have to return a value and what we return on Model function, then that should be reached at the controller variable $data_from_db.

public function getage()
{
  $this->load->database();
  $query = $this->db->query("Select user_name from table_user where user_age= 25");
  $row = $query->row();
  return $row->user_name;
}

Controller can Display the value:


 public function getdata()
  {
   $this->load->Model('ProjectModel');
   $data_from_db =  $this->ProjectModel->getage();
   echo $data_from_db;
  }

Dataflow:

Passing Model data into Controller using Return

Sample output:


Passing Model data into Controller Output

Now the database retrieved name should be shown in our Controller. Normally we will not print anything on controller. So we must move this value into our View section. That we will learn in next lesson.


No comments:

Post a Comment