CodeIgniter Forums
pass data to view from controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: pass data to view from controller (/thread-8361.html)



pass data to view from controller - El Forum - 05-15-2008

[eluser]PHPraja[/eluser]
i have the below code in my constructor of a class in a controller
Code:
$data['mystuffPlaces']=$this->place_model->get_places_mystuff();

and in another function of the same class i have
Code:
$layout['page_title'] = $this->lang->line('My_places');
$layout['body']=$this->load->view('place/user_all_places',$data, True);
$this->load->view('Layout', $layout);

To my surprise i found $data['mystuffPlaces'] is not passed to the layout view.
if i remove it in the constructor and place it in the same function it works!!

or if i replace $data with $this->data, even then though it is in constructor it works!! can any one plz explain me the difference..

Thanks in advance.


pass data to view from controller - El Forum - 05-15-2008

[eluser]GSV Sleeper Service[/eluser]
if $data is being declared in your constructor, then it means that $data is only available within the constructor.

you should be doing something like this -
Code:
class Foo extends Controller {

    var $data = array(); // note: delcared as a class var

    function Foo(){
        $this->data['bar'] = 'wibble';
    }
    
    function other_method(){
        ...
        $layout['body']=$this->load->view('place/user_all_places',$this->data, True);
        $this->load->view('Layout', $layout);
    }

}



pass data to view from controller - El Forum - 05-15-2008

[eluser]PHPraja[/eluser]
Thanks for your reply, another small doubt, if i have another $data as below in other_method() as below
Code:
$data['ss']='test'

should i use $data or $this->data ? plz clarify my doubt.


pass data to view from controller - El Forum - 05-15-2008

[eluser]Pascal Kriete[/eluser]
You still want to use the reference to the class variable $this->data .