php - avoid duplicates in flash based messaging -


i using php session-based flash messenger available here. issue multiple messages of same type when generate errors, display messages, on , fourth. due ajax issues. assuming wanted apply fix in display code here:

public function display($type = 'all', $print = true) {     $messages = '';     $data = '';     if (!isset($_session['flash_messages'])) {         return false;     }     // print type of message?     if (in_array($type, $this->msgtypes)) {         foreach ($_session['flash_messages'][$type] $msg) {             $messages .= $this->msgbefore . $msg . $this->msgafter;         }         $data .= sprintf($this->msgwrapper, $this->msgclass, $this->msgclassprepend.'-'.$type, str_replace('messages', 'autoclose',$this->msgclassprepend.'-'.$type), $messages);         // clear viewed messages         $this->clear($type);     // print queued messages     } elseif ($type == 'all') {         $counter = 1;         foreach ($_session['flash_messages'] $type => $msgarray) {             $count = $counter++;             $messages = '';             foreach ($msgarray $msg) {                 $messages .= $this->msgbefore . $msg . $this->msgafter;             }             $data .= sprintf($this->msgwrapper, $this->msgclass, $this->msgclassprepend.'-'.$type, str_replace('messages', 'autoclose', $this->msgclassprepend.'-'.$type), $messages);         }         // clear of messages         $this->clear();         // invalid message type?     } else {         return false;     }     // print screen or return data     if ($print) {         echo $data;     } else {         return $data;     } } 

how make duplicate messages detected on 1 1 basis. if message "hello" , "hello" , "hello." can remove 1 of first two, , keep later different message speak. workarounds can think of overly complex, , wondering if think of simple solution.

additional info: display encased in class messages , new message created

$msg = new messages(); $msg->add('e', 'some error here.'); 

you run message array through array_unique() before $messages string built. example, these 2 additions display method should trick...

if (in_array($type, $this->msgtypes)) {     $filtered = array_unique($_session['flash_messages'][$type]);     foreach ($filtered $msg) { 

and...

foreach ($_session['flash_messages'] $type => $msgarray) {     $count = $counter++;     $messages = '';     $filtered = array_unique($msgarray);     foreach ($filtered $msg) { 

alternatively, override add method unique check. example

public function add($type, $message, $redirect_to = null, $ignoreduplicates = true) {     // snip...      // wrap array push in check     if (!($ignoreduplicates && in_array($message, $_session['flash_messages'][$type]))) {         $_session['flash_messages'][$type][] = $message; // existing code     }      // snip... } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -