Passing the data array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Passing the data array (/showthread.php?tid=21046) |
Passing the data array - El Forum - 07-29-2009 [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 Passing the data array - El Forum - 07-29-2009 [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)){ 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. Passing the data array - El Forum - 07-29-2009 [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) Passing the data array - El Forum - 07-29-2009 [eluser]Dam1an[/eluser] If you need to pass data between requests, use sessions, or if it's just for the one request, try flashdata Passing the data array - El Forum - 07-29-2009 [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'); ?> Passing the data array - El Forum - 07-29-2009 [eluser]Dam1an[/eluser] I'm assuming you load the session library on both pages? Do you just not get any output? Get an error? Passing the data array - El Forum - 07-29-2009 [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. |