• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pass data to view from controller

#1
[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.

#2
[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);
    }

}

#3
[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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.