Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

Category:Libraries::Message

[h3]Introduction[/h3] This is version 2 of a library I wrote a while back. The first one supported only success, error, notice and warning messages, as well as message groups, but this new library supports anything you want. It's a complete rewrite, much more efficient (about a quarter of the original code).

You can set a message to appear on the current page, or on the next page, such as after a redirect.

You can create message groups, for instance, if you have a feedback form and a newsletter signup form on the same page, you can group your messages so that they're kept separate in the view.

You can set an HTML wrapper around the messages, this is useful if you want to use css and javascript to show the messages overtop of your content.

[h3]Requirements:[/h3] Requires:

  • Sessions - If you set a message to appear on the next page, such as after a redirect, it requires a session.

[h3]Usage:[/h3]

Use of the Message system is quite minimal:

[h4]Load/Autoload Libraries[/h4] [code] $this->load->library('message'); [/code]

[h4]Examples[/h4] [h3]Setting a Message[/h3] [code] // an error $this->message->set('You forgot a required field.', 'error');

// a notice after a redirect $this->message->set('Your account will expire in 5 days.', 'notice', TRUE);

// a success message after a redirect in a specific group $this->message->set('Your subscription has been received.', 'success', TRUE, 'newsletter'); [/code]

[h3]Retreiving Messages and Outputting as HTML[/h3] In your View: [code] $this->message->display(); [/code]

If the message was set in the controller without redirecting, this will display the following:

[code]

You forgot a required field.

[/code]

Or after a redirect:

[code]

Your account will expire in 5 days.

[/code]

To display a group of messages, do this in your view where you want them to display:

[code] $this->message->display('my_group'); [/code]

And if you want to set a wrapper, it requires an array with 2 paramaters. In this example we'll set the group to FALSE, which will give us all non-grouped messages:

[code] $this->message->display(FALSE, array('

', '
')); [/code]

You can also set a default wrapper in a config file. Make a file message.php in your /application/config/ folder, with the following code:

[code] $config['wrapper'] = array('

', '
'); [/code]

[h2]Library 'Message.php'[/h2] Put this in your /application/libraries/ folder: [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**

class CI_Message {

var $CI;
var $messages = array();
var $wrapper = array('', '');

function CI_Message($config){    
    $this->CI =& get_instance();        
    $this->CI->load->library('session');
    
    if($this->CI->session->flashdata('_messages')) $this->messages = $this->CI->session->flashdata('_messages');
    if(isset($config['wrapper'])) $this->wrapper = $config['wrapper'];
}

function set($message, $type, $flash=FALSE, $group=FALSE){
    if(!is_array($message)) $message = array($message);
    foreach($message as $msg){
        $obj = new stdClass();
        $obj->message = $msg;
        $obj->type = $type;
        $obj->flash = $flash;
        $obj->group = $group;
        $this->messages[] = $obj;
    }
    
    $flash_messages = array();
    foreach($this->messages as $msg){
        if($msg->flash) $flash_messages[] = $msg;
    }
    if(count($flash_messages)) $this->CI->session->set_flashdata('_messages', $flash_messages);
}

function display($group=FALSE, $wrapper=FALSE){
    if(count($this->messages)){
        $output = array();
        foreach($this->messages as $msg){
            if($msg->group == $group){
                if(!isset($output[$msg->type])) $output[$msg->type] = array();
                $output[$msg->type][] = $msg->message;
            }
        }
        echo ($wrapper !== FALSE ? $wrapper[0] : $this->wrapper[0])."\r\n";
        foreach($output as $type => $messages){
            echo '<div class="message-'.$type.'">'."\r\n";
            foreach($messages as $msg){
                echo '<p>'.$msg.'</p>'."\r\n";
            }
            echo '</div>'."\r\n";
        }
        echo ($wrapper !== FALSE ? $wrapper[1] : $this->wrapper[1])."\r\n";
    }        
}

} [/code]

Clone this wiki locally