Press "Enter" to skip to content

Sending a Controller Variable into View in CodeIgniter

Sending a variable into View section is the final stage in CodeIgniter architecture. Passing a controller data into view section is simple.

As we shown in previous article, view section is the exact place to displaying contents.

Why do we display contents only on View page:

Only view can customize the contents and there we can place contents anywhere on the page. View page supports HTML, CSS, etc.

Display Controller variable into View CodeIgniter

Now, we successfully retrieved data from database and passed into the Controller. Now let us see how to pass the data from Controller into the View.

You have to create an array of variables to send to view.

For example:

   $data = array(
                  'user_name' => $data_from_db
  • Where $data is the array name.
  • user_name’ is a variable on array that we want to pass into view.
  • $data_from_db is the value to variable ‘user_name’.

Here only one variable is used. Like this, we can create many variable as much we want and comma separate them.


$data = array(
           'user_name' => $data_from_db,
           ‘site_name => ‘Your own codes’   
  • Where ‘site_name’ is another variable and its value is ‘Your own codes’. Note that, you cannot use space on variable name.

Passing Data array into the View:

Now our data array is ready. Let us send this values into the view. It is very simple to send an array of variables into the view. While calling a ‘view page‘ to your controller, you can specify array name with it.

$this->load->view(“user/userdata”) this is the method of calling a view page to a controller. Where userdata is the view page. So let us attach our array with this as a parameter.


  • Where $data is our array name. Now your data has been successfully sent to the view page.
  public function getdata()
$data_from_db =  $this->ProjectModel->getage();
$data = array(
‘user_name’ => $data_from_db,
‘site_name’ => ‘You own codes’


How to Get and Echo Controller Sent Data on View Page

It’s very easy to print controller sent data on your HTML view page. You page should be a PHP page.

Here I created a userdata.php page on View/user folder in CodeIgniter.
Inside the page, you can use any HTML, CSS, JavaScript codes. Aslo you can display array variables by simple echoing their variable names.

In our example we sent two variables named ‘user_name’ and ‘site_name’. So just <php echo $user_name; ?> will show the name. Similarly ‘echo $site_name‘ will show its value, which is ‘You own codes’.


<title> User Details</title>
<center>This is the page body part</center> </br>
  echo $user_name;
echo $site_name;

Where both $user_name and $site_name are the array variables that sent from controller.

Data flow:

Sending Controller variable into View CodeIgniter

Sample output:

Passing Controller data into View CodeIgniter output

Like this, we can fetch row of data from our database, that we will learn in coming lessons.

Be First to Comment

Leave a Reply