Welcome Guest, Not a member yet? Register   Sign In
Data that my 'views' can access which were set in MY_controller, possible?
#1

[eluser]rreynier[/eluser]
Hey guys, is there any way I can make some data set in MY_controller accessible to my views easily?

Right now in MY_controller I am doing something like

Code:
$this->some_variable = $this->some_model->get_some_variable();

Then, in my normal controller, I am loading the view like this:

Code:
$data['some_variable'] = $this->some_variable;
$this->load->view('someview', $data);

Is there any way to add variables directly to my view without having to go about it this way?
#2

[eluser]Dennis Rasmussen[/eluser]
You can create a variable named data in your MY_controller.

Code:
$this->data = array();
$this->data['some_variable'] = $this->some_model->get_some_variable();
$this->data['page_title'] = 'Welcome!';
// ...

Then in your new controller:

Code:
$this->data['foo'] = 'bar';
$this->load->view('someview', $this->data);

That's probably the easiest way to do it.
#3

[eluser]rreynier[/eluser]
That kinda makes sense. So instead of adding to the $data array like I always do.. I will just load additional data to $this->data instead of $data. It would still be cool if there was a way to make stuff available to views from MY_controller.




Theme © iAndrew 2016 - Forum software by © MyBB