Welcome Guest, Not a member yet? Register   Sign In
$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

(This post was last modified: 07-10-2017, 05:49 PM by InsiteFX.)

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 )
Reply
#6

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB