CodeIgniter Forums
Variables not showing up in View - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Variables not showing up in View (/showthread.php?tid=8690)



Variables not showing up in View - El Forum - 05-27-2008

[eluser]floweringmind88[/eluser]
I set a variable in my controller and then I try to print it out in the view and nothing shows up.

If I call a function in the view I can set a variable in the view like so:

$baseurl = $this->base();

but if I have a variable defined in a function of the class I have no access to it in the view.

Am I not loading a helper or is there some configuration I am missing?


Variables not showing up in View - El Forum - 05-27-2008

[eluser]gtech[/eluser]
when you load a view you need to pass in an associative array, the keys then become available as variable names.. like so:

Code:
$dataarr = array();
$dataarr['var1'] = 'value1';
$dataarr['var2'] = 'value2';
$this->load->view('viewname',$dataarr);
then in the view you will have the variables
Code:
<?=$var1?>
<br>
&lt;?=$var2?&gt;
which will print out

value1
value2

note &lt;?=$var1?&gt; is the same as &lt;?php echo $var1 ?&gt;


Variables not showing up in View - El Forum - 05-27-2008

[eluser]floweringmind88[/eluser]
Thanks so much!


Variables not showing up in View - El Forum - 05-27-2008

[eluser]gtech[/eluser]
no problems, the answers to the questions you have been asking is in the documentation somewhere, if you do not want to wade through the docs why not follow the video tutorials linked from the CI homepage, this might give you a good platform to start from.