2
$this->Flash->set('Your college has been updated.');

I am passing flash message like above . Now i want to pass two flash messages. can anyone help me to resolve this issue..

Oops D'oh
  • 941
  • 1
  • 15
  • 34
user12342
  • 107
  • 2
  • 12

2 Answers2

3

You should use [key] param

// In your Controller
$this->Flash->success('The user has been saved', array(
    'key' => 'succ',
    'params' => array(
        'name' => $user['User']['name'],
        'email' => $user['User']['email']
    )
));

$this->Flash->success('The user has been saved', array(
    'key' => 'info',
    'params' => array(
        'name' => $user['User']['name'],
        'email' => $user['User']['email']
    )
));
// in View
echo $this->Flash->render('succ');

echo $this->Flash->render('info');
Quy Le
  • 2,354
  • 25
  • 18
1

You can check here https://mrphp.com.au/blog/multiple-flash-messages-with-style-in-cakephp/ In this CakePHP tutorial I will explain how to output multiple flash messages.

This forms a message stack that can be used to inform the user that multiple events have taken place. The Multi-Flash Function

We want to add a method to your app_controller to handle the message stack. Vary it to suit your needs

app_controller.php

<?php
class AppController extends Controller {
    function _flash($message,$type='message') {
        $messages = (array)$this->Session->read('Message.multiFlash');
        $messages[] = array(
            'message'=>$message, 
            'layout'=>'default', 
            'element'=>'default',
            'params'=>array('class'=>$type),
        );
        $this->Session->write('Message.multiFlash', $messages);
    }
}

Controller Method

This is just a test action to view your messages.

controllers/posts_controller.php

<?php
class PostsController extends AppController {
    var $name = 'Posts';
    function admin_index() {
        $this->_flash(__('Normal message.', true),'message');
        $this->_flash(__('Info message.', true),'info');
        $this->_flash(__('Success message.', true),'success');
        $this->_flash(__('Warning message.', true),'warning');
        $this->_flash(__('Error message.', true),'error');
        $this->set('posts',$this->paginate());
    }
}

Display The Messages

We want to display a message to the user for message we set.

views/layouts/default.php

<div id="messages">
<?php
    if ($session->check('Message.flash')) $session->flash(); // the standard messages
    // multiple messages
    if ($messages = $session->read('Message.multiFlash')) {
        foreach($messages as $k=>$v) $session->flash('multiFlash.'.$k);
    }
?>
</div>

Some CSS to Make it Pretty

This will set some colours and images.

.message, .info, .success, .warning, .error {
    border: 1px solid;
    margin: 10px 0px;
    padding:15px 10px 15px 65px;
    background-repeat: no-repeat;
    background-position: 10px center;
    font-size: 125%;
}
.info {
    /* color: #00529B; */
    border-color: #00529B;
    background-color: #BDE5F8;
    background-image: url('../../img/message/info.png');
}
.success {
    /* color: #4F8A10; */
    border-color: #4F8A10;
    background-color: #DFF2BF;
    background-image:url('../../img/message/success.png');
}
.warning {
    /* color: #9F6000; */
    border-color: #9F6000;
    background-color: #FFFABF;
    background-image: url('../../img/message/warning.png');
}
.error {
    /* color: #D8000C; */
    border-color: #D8000C;
    background-color: #FFBABA;
    background-image: url('../../img/message/error.png');
}
Er.KT
  • 2,852
  • 1
  • 36
  • 70
  • I want to display only two messages example info and success. Info at the bottom and success at the top. how to display both without using foreach . ex: $this->Session->setFlash('info'); $this->Session->setFlash('success');. I want to display like this – user12342 Jan 05 '16 at 10:32