CodeIgniter Forums
What happens to $data when it is passed to the view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: What happens to $data when it is passed to the view? (/showthread.php?tid=61596)



What happens to $data when it is passed to the view? - n2fole00 - 04-29-2015

Could someone help me get my head around what happens when you pass $data to the view?

For example, in the controller I have...

PHP Code:
$data['getMessages'] = $this->test_model->get_messages(); 
This calls the method in Test_model and assigns it to an array of stdClass objects called [getMessages] which is inside the array $data.

Then I pass the $data array to the view...

PHP Code:
$this->load->view('/test/index'$data); 
...but I can access the array in the view without the $data container like $getMessages instead of $data['getMessages'][0]->messages (which causes an error).

What happens to $data for it to be accessible with $getMessages in the view?

Thanks.


RE: What happens to $data when it is passed to the view? - massaki - 04-29-2015

In the view, it is extracted from the array, and turns to the variable $getMessages.
Then, you should access your data like this: $getMessages[0]->messages


RE: What happens to $data when it is passed to the view? - swand - 04-29-2015

Hi,

probably you in get_messages(), you are returning object form rather than and array form. Check documentation and look into difference for result() and result_array()

http://www.codeigniter.com/user_guide/database/results.html

I hope this helps.


RE: What happens to $data when it is passed to the view? - davidgv88 - 04-29-2015

Hi!

You can do this:

$return = array();
$data = array();

$data['getMessages'] = $this->test_model->get_messages();
$data['foo'] = 'bar';

$return['data'] = $data;
$this->load->view('/test/index', $return);

Now in view you can access to $data['getMessages'] or $data['foo']


RE: What happens to $data when it is passed to the view? - nekalv - 04-29-2015

(04-29-2015, 08:14 AM)n2fole00 Wrote: Could someone help me get my head around what happens when you pass $data to the view?

For example, in the controller I have...


PHP Code:
$data['getMessages'] = $this->test_model->get_messages(); 
This calls the method in Test_model and assigns it to an array of stdClass objects called [getMessages] which is inside the array $data.

Then I pass the $data array to the view...


PHP Code:
$this->load->view('/test/index'$data); 
...but I can access the array in the view without the $data container like $getMessages instead of $data['getMessages'][0]->messages (which causes an error).

What happens to $data for it to be accessible with $getMessages in the view?

Thanks.


When you pass data to the view, every item in the array you pass becomes a variable so you must access to it from the view like this. $getMessages->message


RE: What happens to $data when it is passed to the view? - mwhitney - 04-29-2015

Eventually, the $data is passed (along with variables set through other methods) to extract(), which imports the values to variables named for the associated keys in the array and exposes them for use in your view.


RE: What happens to $data when it is passed to the view? - CroNiX - 04-29-2015

$data['getMessages'] = $this->test_model->get_messages();

becomes $getMessages in the view. As mwhitney mentioned, CI runs extract() on the array passed to the viewfile.
http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view


RE: What happens to $data when it is passed to the view? - cartalot - 04-29-2015

just as an fyi you can also use $this->something to pass data to the view.
i think in general its better to pass it explicitly with $data but there are times when $this->
gives you more flexibility.  

PHP Code:
// in controller
$this->dailyGreeting $this->test_model->get_dailyGreeting(); 

// in view
echo $this->dailyGreeting 

also works great for development or error messages because you set it in any controller / model method
and it will show up in the view(s) that are called.

PHP Code:
// if the greeting is not returned, create a developer message
if( ! $this->dailyGreeting $this->test_model->get_dailyGreeting() ){

 
  $this->dev_error .= "Error returning daily greeting" ; }

// development view
 
echo $this->dev_error 



RE: What happens to $data when it is passed to the view? - n2fole00 - 04-29-2015

Thanks folks, that really helped.