• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$data array becomes variables

#1
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?  
Reply

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

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

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

#4
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.
Reply

#5
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 the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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