Welcome Guest, Not a member yet? Register   Sign In
Variables not showing up in View
#1

[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?
#2

[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;
#3

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB