$data array becomes variables |
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?
Yes, the second parameter of view() is where you put your array:
$this->load->view('whatever', $array );
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.
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I just wanted to acknowledge and thank you for posting to this thread. Your answer helped a lot.
CodeIgniter extracts the $data array() to separate variables. but you can use second parameter of view() is where you put your array.
|
Welcome Guest, Not a member yet? Register Sign In |