![]() |
Simple Flashdata extension to retrieve array of flashadata based on key. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Simple Flashdata extension to retrieve array of flashadata based on key. (/showthread.php?tid=52700) |
Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]Grimbog[/eluser] Hi guys, Just thought to share this as it may be useful in certain cases - at the moment session->flashdata is only returned as a single string, but in my own case I needed a way to show multiple notifications to the screen at once. Why? Lets say that you submit a form, and it goes away to do its thing... not only do I have form validation going on, but there is also the process of accessing the database and checking data, or processing user input. Rather than having just 1 ambiguous line of text saying "an error has occurred", I prefer to have types of notifications in the following categories: notice, success, pending, warning, error So for each function that's run, it attempts to carry its task out and creates a global notifications array filled with types of messages like so: Code: core()->set_notification( 'The login you provided appears to be invalid.', 'warning'); Code: core()->set_notification('Sorry, an error has occurred submitting this form.', 'error'); Don't worry about core(), this is just a master class library I wrote for my own app, but the function set_notification() is as follows: Code: /** So the function populates a global array called 'notifications', that is then filled with the message and error type. Finally this global array is sent to the session->set_flashdata. Now, we still can't do anything nice, because the stock set_flashdata function for CI was not designed to traverse multidimensional arrays. So, the trick is to extend the Session class with MY_Session.php and do a few changes. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); So here, we have 'set_flashdata' which has been altered slightly so that it checks if the value of $newdata is actually an array itself (taken from the global notifications array). It then traverses the multiarray and generates the flastdata key with the numeric key value from the global notifications to ensure the flashdata keys are kept unique and not overwritten. The 'keep_flashdata' is basically altered so that I can still set which key I want to keep for another run/process. So because we simply look for the :old: value and then convert it to :new: we can safely split it via explode. The final function 'flashdata' just ensures that we retain the array when we push it to view, rather than a single string. Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]Grimbog[/eluser] Please note, in your view file you need to set up flashdata like so: Code: <?php if($this->session->flashdata('notifications')) : ?> Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]marcogmonteiro[/eluser] So with this I'll be able to have 2 flashdata messages of the same type? I can do this? Code: set_notification( 'The login you provided appears to be invalid.', 'warning'); Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]Grimbog[/eluser] [quote author="marcogmonteiro" date="1340358723"]So with this I'll be able to have 2 flashdata messages of the same type? I can do this? Code: set_notification( 'The login you provided appears to be invalid.', 'warning'); [/quote] Hey Marco, Yes, because the global array keys for notifications are created on the fly (0,1,2 etc.), and then merged with the multiarray key name (notifications), so it creates a new key using both the numeric and string keys (notifications1). This is then sent to userdata as '[flash:old:notifications0], [flash:old:notifications1]' etc. etc. Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]Grimbog[/eluser] Should also say that you'll need to have some sort of global controller (MY_Controller.php) with the 'set_notification' function placed in and the following... Code: public $notifications = array(); ... placed before the constructor as a property declaration. set_notification accesses it with $this->notifications[]. Simple Flashdata extension to retrieve array of flashadata based on key. - El Forum - 06-22-2012 [eluser]marcogmonteiro[/eluser] Aye, sounds awesome man! Good job... I might have some use for this. =) |