Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter : pass data to a view included in a view

I have a controller and including two views from one function as below

$this->load->view('includes/header',$data);
$this->load->view('view_destinations',$data);

The view file view_destinations.php including a php menu file as follows

<? $this->load->view('includes/top_menu'); ?>

My question is, how can I pass data that is fetched from the controller to this included top_menu.php ?

Thank you guys

like image 834
Thomas John Avatar asked Dec 01 '10 14:12

Thomas John


2 Answers

Inside your controller, have

$data['nestedView']['otherData'] = 'testing';

before your view includes.

When you call

$this->load->view('view_destinations',$data);

the view_destinations file is going to have

$nestedView['otherData'];

Which you can at that point, pass into the nested view file.

<? $this->load->view('includes/top_menu', $nestedView); ?>

And inside your top_menu file you should have $otherData containing 'testing'.

like image 114
castis Avatar answered Oct 20 '22 15:10

castis


castis's solution works

however if you want to do this on a more finely grained level you can use:

//in your controller
$data['whatever'] = 'someValue';

.

//In your view
echo $whatever //outputs 'someValue';

//pass $whatever on
$this->load->view('some/view', Array('whatever' => $whatever));
like image 30
thelastshadow Avatar answered Oct 20 '22 16:10

thelastshadow