• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Message Handling Library

#1
[eluser]Unknown[/eluser]
Hey guys,

i thought i would post a simple message handling library i came up with. Nothing special but maybe a good foundation for something more involved.

currently it has multiple states of a message (success, warn & error).

Will be making more improvements soon!

should be located in: application/libraries/Message_handler.php

probably a good idea to autoload in autoload.php

code below:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Message_handler {

    var $CI;
    
    function Message_handler() {
        $this->CI =& get_instance();
        log_message('debug', 'Message Handler initialized.');
        
        $this->CI->load->library('session');
    
    }
    
    //show and remove (page scope only)
    
    function display() {
        
        $msg = $this->CI->session->userdata('message');
        
        $this->CI->session->set_userdata(array('message'=>''));
        
        return $msg;
        
    }
    
    // add a message for display
    function add($msg,$type = 'success') {
        
        switch($type) {
            case 'success':
                $output  = '<div id="success">';
            break;
            
            case 'error':
                $output  = '<div id="error">';
            break;
            
            case 'warning':
                $output  = '<div id="warning">';    
            break;
        }
        
        
        $output .= '<div class="item">' . $msg . '</div>';
        $output .= '</div>';
        
        $this->CI->session->set_userdata(array('message'=>$output));
        
    }

}

?&gt;

in your controller simply use line to add a message:
Code:
$this->message_handler->add('Your account has been created.');

to display a message use this code, in your view or passed from a controller:
Code:
$this->CI->message_handler->display()

cheers all (codeigniter FTW)

#2
[eluser]wemago[/eluser]
hmm that looks just like flashmsgs that you can use in sessions Wink
just check the svn for the new CI updates.

#3
[eluser]Unknown[/eluser]
ill have to check that out

cheers!

#4
[eluser]Craig A Rodway[/eluser]
That's a nice idea, thanks! Smile

This might be useful for styling your boxes: Create a valid CSS alert message (read the comments for some discussion on correct CSS usage though).

Mu current method for these sort of messages is like this:

4 view files (error, info, warning, question) in a directory called msgbox. This is my /views/msgbox/error.php file:

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

Although this is not the traditional use for the variable passing to views, it allows me to do this in my controllers and/or views:

Code:
// Controller:
$body['content'] = $this->load->view('msgbox/error', 'An error occured.', True);

// View:
$this->load->view('msgbox/error', 'An error occured');

#5
[eluser]koryrok[/eluser]
to display a message use this code, in your view or passed from a controller:
Code:
$this->CI->message_handler->display()

Just want to make a quick correction to the code above. It should be:
Code:
$this->message_handler->display()


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.