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


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?


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:
    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.

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)

If you need to pass data between requests, use sessions, or if it's just for the one request, try flashdata

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->session->set_flashdata('message', 'You have been successfully logged out.');
redirect('/user/login/', 'refresh');

And then this in my login view

echo $this->session->flashdata('message');

I'm assuming you load the session library on both pages?
Do you just not get any output? Get an error?

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:


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