CodeIgniter Forums
$data array becomes variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: $data array becomes variables (/showthread.php?tid=68431)



$data array becomes variables - danallen - 07-10-2017

Am I understanding correctly?  I have seen controllers creating an array called $data with all the data needed by a view to do its job.  When the view is loaded, the elements of the array have been converted into variables.  For example, in a controller, I see:

PHP Code:
$data['product_code']='pc'

After a view is loaded, there is no array named $data, but there is a variable named $product_code, set to the same value as $data['product_code'].  

Is this something done by CodeIgniter?  


RE: $data array becomes variables - skunkbad - 07-10-2017

Yes, the second parameter of view() is where you put your array:

$this->load->view('whatever', $array );


RE: $data array becomes variables - Wouter60 - 07-10-2017

Yes, CodeIgniter extracts the $data array() to separate variables.


RE: $data array becomes variables - dave friend - 07-10-2017

As the other have said, CodeIgniter extracts the $data array() to separate variables. Specifically the loader class does it by calling the PHP function extract. Read about extract HERE.

One thing that is easily missed about data sent to "views" is that the data remains available to subsequent calls to load->view(). In other words, you don't have to keep sending the same data over and over again if additional views will use the data provided earlier.


RE: $data array becomes variables - InsiteFX - 07-10-2017

And if you do not like passing the $data array in the view you use $this->load->vars($data);

This will make it global to all views.


RE: $data array becomes variables - danallen - 10-03-2017

I just wanted to acknowledge and thank you for posting to this thread. Your answer helped a lot.


RE: $data array becomes variables - Tiffansy - 11-17-2017

Yes, the second parameter of view() is where you put your array


RE: $data array becomes variables - XtreemDeveloper - 12-20-2017

CodeIgniter extracts the $data array() to separate variables. but you can use second parameter of view() is where you put your array.