Welcome Guest, Not a member yet? Register   Sign In
Session Flash Data, pointless?
#11

[eluser]adamp1[/eluser]
This is my solution, works exactly the same as flashdata and its for passing messages to the page. So you cans set either info/success/error/warning messages. You can set as many as you want since there stored in an array.

Then when you call display() it clears the messages stored and outputs the messages to the view file specifed in the class vars.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* BackendPro
*
* A website backend system for developers for PHP 4.3.2 or newer
*
* @package            BackendPro
* @author                Adam Price
* @copyright            Copyright (c) 2008
* @license                http://www.gnu.org/licenses/lgpl.html
* @tutorial                BackendPro.pkg
*/

// ---------------------------------------------------------------------------

/**
* Status
*
* Handles the Status messages displayed to a user
*
* @package            BackendPro
* @subpackage        Library
* @tutorial                Status.cls
*/
    class Status
    {
        var $flash_var = "status";
        var $types = array('info','warning','error','success');
        var $view_file = "status.php";

        /**
         * Constructor
         */
        function Status()
        {
            // Get CI Instance
            $this->CI = &get;_instance();

            log_message('debug','Status Class Initialized');
        }

        /**
         * Set new status message
         *
         * The message will be live untill $this->display() is called
         *
         * @acces public
         * @param string $type Type of message to set
         * @param string $message Message to display
         * @return boolean
         */
        function set($type = NULL, $message = NULL)
        {
            if ( $type == NULL OR $message == NULL){
                return FALSE;
            }

            // Check its a valid type
            if ( !in_array($type,$this->types) ) {
                show_error("'".$type."' is not a valid status message type.");
            }

            // Fetch current flashdata from session
            $data = $this->_fetch();

            // Append our message to the end
            $data[$type][] = $message;

            // Save the data back into the session
            $this->CI->session->set_userdata($this->flash_var,serialize($data));
        }

        /**
         * Display status messages
         *
         * If no type has been given it will display every message,
         * otherwise it will only show and remove that certain type of
         * message
         *
         * @access public
         * @param string $type Error type to display
         * @return string
         */
        function display($type = NULL)
        {
            $msgdata = $this->_fetch();

            // Output variable
            $output = "";

            if ( $type == NULL ) {
                // Display all messages
                foreach ( $msgdata as $key => $mtype )
                {
                    $data['messages'] = $mtype;
                    $data['type'] = $key;
                    $output .= $this->CI->load->view($this->view_file, $data, TRUE);
                }
            }
            else {
                // Only display messages of $type
                $data['messages'] = $msgdata[$type];
                $data['type'] = $type;
                $output =  $this->CI->load->view($this->view_file, $data, TRUE);
            }

            // Remove messages
            $this->_remove($type);

            return $output;
        }

        /**
         * Unset messages
         *
         * After a message has been shown remove it from
         * the session data.
         *
         * @access private
         * @param string $type Message type to remove
         * @return void
         */
        function _remove($type = null)
        {
            if ( $type == null) {
                // Unset all messages
                $this->CI->session->unset_userdata($this->flash_var);
            }
            else {
                // Unset only messages with type $type
                $data = $this->_fetch();
                unset($data[$type]);
                $this->CI->session->set_userdata($this->flash_var,serialize($data));
            }
            return;
        }

        /**
         * Fetch flashstatus array from session
         *
         * @access private
         * @return array containing the flash data
         */
        function _fetch()
        {
            $data = $this->CI->session->userdata($this->flash_var);
            if ( empty( $data ) ) {
                return array();
            }
            else {
                return unserialize($data);
            }
        }
    }
?>

With this library you can show all messages using the same design. All you need to enable form validation errors is to loop around the errors and add set a new message using the method above.

An example of the view file is as follows:
Code:
<div  id="FlashStatus" class="&lt;?=$type?&gt;">
    <h6>&lt;?=ucfirst($type)?&gt;</h6>
    <ul>
    &lt;?php
        foreach($messages as $text)
        {
            print "<li>" . $text . "</li>";
        }
    ?&gt;
    </ul>
</div>
#12

[eluser]Craig A Rodway[/eluser]
I find flashdata useful. This is how I mainly use them for anybody interested.

I have some 'partial' view files (views/msg/err.php, views/msg/info.php ...) which contain:

Code:
<p class="msg err">&lt;?php echo $vars; ?&gt;</p>

and have the base CSS class 'msg' applied to style the layer, and the 'err' class to apply a red colour to it. Obviously the latter style is different in each file.

As an example, my controller function for saving canges to a user:

Code:
...
$msg = "User was saved successfully.";
$this->session->set_flashdata('msg', $this->load->view('msg/err', $msg, TRUE));
redirect('admin/users');

In my core template I echo $this->session->flashdata('msg');.

Simple Smile
#13

[eluser]xwero[/eluser]
[quote author="Rick Jolly" date="1201044620"]xwero, I think you're missing Michael's point. Flashdata "success" messages are good to use in combination with a redirect.

If a user makes an error filling out a form then the page is redisplayed with errors - no redirect so no need for storing the errors in the session. However, when the form is submitted with no errors, then often the user will be redirected to a success page. Of course, in order for the success page to display a message, it needs to get the message from the session, or create a message in response to some url segment parameter.[/quote]
I missed that point indeed so there are uses for flashdata Smile
#14

[eluser]Michael Wales[/eluser]
I like that class adamp - I was working on something remarkably similar last night on a current project. I am using the default flash data but then have a library wrapper that will determine if there are any flashes, what type they are (notice, warning, info, error) and then load a view to contain that information.
#15

[eluser]Andy Blackwell[/eluser]
I wrote up a similar class a while back that comes with a default style. Edit the css/images and you can have your own style to it.
Here's the thread: FlashNotice

Feel free to steal anything you like out of there! ;-)
#16

[eluser]adamp1[/eluser]
Thanks Andy, that's where I got most of my ideas from for my class. The only reason I didn't use it was the html was built into the controller (1.1 wasn't out then).
#17

[eluser]ntheorist[/eluser]
check out the jGrowl plugin for jQuery. I use in combination with flash messages and it works pretty well. Using the 'theme' option you can easily create css styles for error,notice,warning etc.

n




Theme © iAndrew 2016 - Forum software by © MyBB