Welcome Guest, Not a member yet? Register   Sign In
Passing the data array
#1

[eluser]cz231[/eluser]
Hi,

I'm relatively new to codeigniter, and as such am still learning some of the principles of it. Recently, I was trying to pass a variable to the view from the controller with the data array. Specifically, I wanted to pass a message to the view. But currently, with the way I have it set up, if the user accesses the page without getting the variable assigned anything, ci throws an error.

How do I set an automatic empty value for the variable that will only get overrode if something specific is assigned?

Thanks
#2

[eluser]wowdezign[/eluser]
If I understood you correctly, you only want to take action if the variable exists in the view.

I that is correct, depending on the rest of the code, I normally use something like:
Code:
if(isset($varFromController)){
    echo $varFromController;
}

In my view file.

This way, it only gets written if the variable is set, that wat I don't have to pass the variable in from every controller.
#3

[eluser]cz231[/eluser]
Thanks for the quick reply!

That will work perfectly. One more question: How do I pass the data variable without loading a view? (In this case, I have a redirect)
#4

[eluser]Dam1an[/eluser]
If you need to pass data between requests, use sessions, or if it's just for the one request, try flashdata
#5

[eluser]cz231[/eluser]
Flashdata sounds like what I want, however I'm having some issues with it working.

I have this in my controller:

public function logout ()
{
$this->redux_auth->logout();
$this->session->set_flashdata('message', 'You have been successfully logged out.');
redirect('/user/login/', 'refresh');
}

And then this in my login view

<?php
echo $this->session->flashdata('message');
?>
#6

[eluser]Dam1an[/eluser]
I'm assuming you load the session library on both pages?
Do you just not get any output? Get an error?
#7

[eluser]cz231[/eluser]
Yeah I auto-loaded it.

It works if I'm loading views, but not if I redirect. Is that because I have to keep the flashdata for another step? So for example, I set the flashdata then I redirect. Do I have to put something like this:

$this->session->keep_flashdata('message');

In the controller I'm redirecting to? Or where do I put that line?

Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB