CodeIgniter Forums
Accessing controller variables in the viewer - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Accessing controller variables in the viewer (/showthread.php?tid=2715)



Accessing controller variables in the viewer - El Forum - 08-21-2007

[eluser]dedavai[/eluser]
I'm having trouble accessing variables created inside the controller from within the viewer. For example, inside the controller (home.php) I have the following:
Code:
function view() {
...
$sub = false;
$this->load->view('home_view');
}
and inside the view file (home_view.php) I have
Code:
<input type="text" name="cp" value="<?=($sub ? "CP value" : "");?>">
However, the $sub never makes it from the controller to the viewer. Is there a special way to reference the variables ?


Accessing controller variables in the viewer - El Forum - 08-21-2007

[eluser]Michael Wales[/eluser]
Variables must be passed within an array to the view from the controller.

Controller:
Code:
function view() {
  $data['sub'] = FALSE;
  $this->load->view('home_view', $data);
}

View:
Code:
<input type="text" name="cp" value="<?= $sub ? "CP Value" : ""); ?>">

Please read the View documentation and the Loader documentation.