I have a function in one of my views, and I want to access one of the variables available to the view through CodeIgniter's data array.
For example; in my controller I have this:
$this->load->view('someview', array(
'info' => 'some info'
));
Now, within my view, I have a function, and I want to be able to access the variable $info
from within that function scope.
Is that possible?
in your controller:
Your function:
I tried this but using
globals
in my viewsomeview.php
doesn't work..You may need to pass this as a parameter instead to your function so it's available to it.
I read this method
$this->load->vars($array)
in http://codeigniter.com/user_guide/libraries/loader.html
but it's purpose is just to make it available to any view file from any function for that controller. I tried my code above
global $info;
and it still doesn't work.You may need to do the workaround by passing it as a parameter instead.
Try including this in your
someview.php
=>print "<pre>";print_r($GLOBALS);print "</pre>";
and the variables passed through$this->load->view
aren't included.I solved this by creating a global variable within the view, then assigning the passed in $data value to the global variable. That way, the information can be read within the view's function without having to pass the variable to the function.
For example, in the controller, you have:
Then in the showpage.php view, you have:
Hope this helps!