Welcome Guest, Not a member yet? Register   Sign In
Variable from Controller not working in Views
#1

[eluser]Kraig[/eluser]
Why is the variable test_var not working in the view like the guide shows? Variable is created at the end.
Controller:
Code:
class Members_area extends CI_Controller {

function index()
{
  $is_logged_in = $this->loggedin->loggedin();
  if(!$is_logged_in)
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template2', $data);
  }
  else
  {
   $data['main_content'] = 'members_area';
   $this->load->view('includes/template2', $data);
  }
  
  // Grabbing all the files associated with the current user
  $this->load->model("upload_model");
  $q = $this->upload_model->get_files();
  $data['test_var'] = 'Test'; // Trying to use this variable in the views file

}

}

The View File:
Code:
<li>&lt;?php echo $test_var;?&gt;</li>

I get undefined variable as an error
#2

[eluser]xtremer360[/eluser]
That's because you have to define the data array key before you send the data array to the view file. So what I would suggest is sense both the key values are the same that are inside the if and else statements I suggest you take it out of it and move it to the bottom that way you only have to have that line once that way those bottom three lines will all work for you.
#3

[eluser]Kraig[/eluser]
Not sure what you mean...
#4

[eluser]xtremer360[/eluser]
Try this:

Code:
class Members_area extends CI_Controller {

function index()
{
  $is_logged_in = $this->loggedin->loggedin();
  if(!$is_logged_in)
  {
   $data['main_content'] = 'home';
  }
  else
  {
   $data['main_content'] = 'members_area';
  }
  
  // Grabbing all the files associated with the current user
  $this->load->model("upload_model");
  $q = $this->upload_model->get_files();
  $data['test_var'] = 'Test'; // Trying to use this variable in the views file
  $this->load->view('includes/template2', $data);
}

}
#5

[eluser]Kraig[/eluser]
Thanks!!
#6

[eluser]Kraig[/eluser]
Had to change it slightly, but it worked!




Theme © iAndrew 2016 - Forum software by © MyBB