Welcome Guest, Not a member yet? Register   Sign In
Variables from Model to Controller
#1

[eluser]Blackparadox[/eluser]
Hello,

I'm new to CodeIgniter (kinda new in PHP world also) and I tried the following thing: sending a variable from a model to a controller. The model is loaded in the controller but the variable isn't loaded (i tried return $var; and $this->load->vars(array('var' => $var))).
Using $this, I can access $var in my view, but not in controller. Is there any way to send the variable to the controller?

Thanks!
#2

[eluser]TWP Marketing[/eluser]
Please show your code.

The controller might use something like this:
Code:
...
$data['somevalue'] = $this->Some_model->get_a_value();

$this->load->view('myview',$data);
...

In the view, you may access a var named $somevalue, which was exploded from the $data array
Code:
...
<?php echo $somevalue;?>
...
#3

[eluser]Blackparadox[/eluser]
The problem isn't accessing the variable from the view, my code is a bit different...

Code:
$this->load->vars(array('var' => $var))

and in view I can easily do

Code:
echo $var;

But if I do the same thing in controller says the variable is undefined. I know, I shouldn't echo in controller but I don't want to do that, it's just and example
#4

[eluser]TWP Marketing[/eluser]
Shoving a piece of data to the browser from the controller breaks the NVC pattern, but I think you would need to address the var in the current instance:

Code:
$this->var;

#5

[eluser]Blackparadox[/eluser]
Still doesn't works... probably somebody has another idea if I explain how my script works.

In my model I have a function called login_check(). If user is logged in, it pulls all its info in $fetch. I want to access $fetch in every controller where login_check() is used. Should I store the value (array) of $fetch in the user session? Will the speed of the script will be affected if I do so?
#6

[eluser]TWP Marketing[/eluser]
Session data is the place to store the information, since you are accessing it from more than one controller.
Speed will be effected, but not greatly since you already have session data being managed. Test it?




Theme © iAndrew 2016 - Forum software by © MyBB