Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

How to Pass Data From Controller to View in CodeIgniter

Updated on August 9, 2017

3 Min Read
Reading Time: 3 minutes

A view is webpage that displays all the elements of the UI. In many cases, view is often a fragment of page (such as header, footer, widget areas and sidebars). In many cases, views can be embedded in other views. One important aspect of views is that views could not be called directly. You need to load the views through a controller. In this tutorial, I will highlight the simple, and yet very important process of how to pass data in CodeIgniter application deployed on any Web Hosting for PHP.

pass data in codeigniter from controller to view

You might also like: How To Host CodeIgniter PHP On Cloud Using SSH

Create a View

Create a new text file and name it cwblogview.php. Save this file in application/views/ directory. Open the file and add the following code to it:

Load the View

Loading a view is usually executed through the following syntax:

where ‘name’ is the name of the view

Noe create a controller with the name Blog.php.  This controller will contain the method for loading the view:

Once you have created the controller, the URL for the view would be:

Load Multiple Views

Within a controller, CodeIgniter could handle multiple calls to the view ($this->load->view()). Check out the following code:

Sort Views in Subfolders

It is easy to sort views within subfolders:


Add Dynamic Data to Views

The usual route of data transfer from controller to view is through an array or an object. The usual case is that the array or the object is passed as the second parameter of the view load method (something like the following):

The controller would look like this:

This is what the the view file would look like:

Create the Loops

Notice that the data array that is passed to the view files could be both simple variables and multi-dimensional arrays. In the case of multi-dimensional arrays, you could setup loops to generate multiple rows. For this, add the following code in the controller:

Next, open up the view file and create a loop:

Returning View as Data

The behavior of this method could be further customized through an optional third parameter. Using this parameter returns data as a string instead of sending it to the browser. This is important if you need to have a dataset for processing. Setting the third parameter to TRUE will return the data. The default behavior is FALSE that sends it to the browser.

Using the third parameter, the syntax would be:

You might also like: Pass Data From One Function To Another In Same Codeigniter Controller


In this tutorial I have discussed how to pass data from controller to view in CodeIgniter. The process is very simple and you could easily render views through the controller.

If you need help, you could add a comment below describing your issue. I will get back to you ASAP!

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.