Passing variable from controller to view in CodeIgniter

You have to pass an array to the view. CodeIgniter automatically makes $planet available to you.

$data = array('planet' => $planet);
$this->load->view('main_view', $data);

With this you can use $planet in your view.

E.g., if you do the following:

$data = array('foo' => 'Hello', 'bar' => 'world');
$this->load->view('main_view', $data);

$foo and $bar will be available in your view. The key-value pairs in the array are automatically converted to variables in the view.

Leave a Comment