Part of the EllisLab Network
This thread is a discussion for the wiki article: Messages
   
 
Messages
Posted: 09 July 2008 11:36 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-08-2008

how to implement in controller??

function login() {
  $this->load->library(array(‘session’,‘messages’));
  $this->load->helper(‘form’);
  if($_POST == true){
    $username=$this->input->post(‘username’);
    $password=$this->input->post(‘password’);
    if(empty($username))$this->messages->add(‘Username harus diisi !’, ‘error’);
    if(empty($password))$this->messages->add(‘Password harus diisi!’, ‘error’);
  }
   
$data[‘messages’] = $this->messages->get();

}

if ($this->messages->get()) {
      echo “test”;
    $this->init_model->displayTemplate(‘front/login’, $data);
}else{
  echo “test2”;
$this->init_model->displayTemplate(‘front/login’, $data);
}

thk all..

Profile
 
 
Posted: 09 July 2008 11:54 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  140
Joined  04-24-2007

Here is the way I used it:
in the controller:

$this->messages->add($this->validation->error_string'error');
      
$this->loadview('admin/category/add'$this->data); 

in the view:

<?=output_messages(); ?> 

output_messages is a function in a helper:

<?php
function output_messages($type null)
{
  $CI 
=& get_instance();
   
  if (
$CI->messages->sum($type) > 0)
  
{
    $messages 
$CI->messages->get($type);
    
// display all messages of the type
    
if (is_array($messages))
    
{
        $output 
'';
        foreach (
$messages as $type => $msgs)
        
{
            
if (count($msgs) > 0)
            
{
                $output 
.= '<div class="' $type '">';
                
$output .= '<ul>';            
                foreach (
$msgs as $message)
                
{
                      $output 
.= $message;
                
}
                $output 
.= '</ul>';
                
$output .= '</div>';
            
}
        }
    }
    
return $output;
  
}
 Signature 

CI Bookmarks

Profile
 
 
Posted: 10 July 2008 11:15 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-08-2008

thank you ontguy, but I did not really understand about your explenation.
please send me complete sample of MVC for login application.

thank for your attention.

Profile
 
 
Posted: 11 July 2008 10:21 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  140
Joined  04-24-2007

With my previous reply in mind: I add the function output_messages to all views I expect to have a message. The view ‘catalog/browse’ has the function output_messages, so after a successful login that message would be seen.

I don’t think there’s a need for success and failure views if you’re using this library. Just add a message with what you would have included in the view.

controller - login.php

$this->_load_login_validation();
    
    
//valid login checked in MY_Validation.php
    
if ($this->validation->run() === FALSE)
    
{
      $this
->messages->add($this->validation->error_string'error');                 
          
$this->loadview('user/login'$this->data);
        
}
        
else
        
{
          $this
->messages->add('successful login message''success');
      
      
//could also check for a "referring" page and redirect to that
      
redirect ('catalog/browse');    
    

 
view - login.php

<h2>Login</h2>
<?=output_messages(); ?>
<?
=form_open('login'); ?>
    
<label for="username">Username:</label
  <
input type="text" name="username" id="username" value="<?=$this->validation->username; ?>" /><br />
  <
label for="password">Password:</label>
    <
input type="password" name="password"id="password"  value="<?=$this->validation->password; ?>" /><br /><br />
    <
input type="submit" name="submit" value="Login" id="submit" />
<?=form_close(); ?> 
 Signature 

CI Bookmarks

Profile
 
 
Posted: 12 July 2008 05:26 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-08-2008

in my controller

function login() {
               $this
->load->library(array('validation','messages'));
               
$this->load->helper('form');
               
$data['title']="Smartindo-Technology";
               
               
$rules['username''trim|required';
               
$rules['password''trim|required';
               
$this->validation->set_rules($rules);
               
               
$fields['username']    'Username';
               
$fields['password']    'Password';
               
$this->validation->set_fields($fields);
                
                if (
$this->validation->run() == FALSE)
                
{
                       $this
->messages->add($this->validation->error_string'error');
                       
$this->init_model->displayTemplate('front/login'$data);
                
}
               
else
               
{
                        $this
->messages->add('Successful login''success');
                        
$this->init_model->displayTemplate('front/login'$data);
                
}
        } 

in my view:

<table width="100%">
    <
tr><td><?=output_messages();?> </td></tr>
</
table>
<?=form_open("user/login/"?>

<table width="100%" cellpadding="0" cellspacing="0">
    <
tr valign="top">
       <
td width="20%">Username</td>
      <
td width="1%">:</td>
      <
td width="79%"><input type="text" class="textfield" name="username" id="username" value="<?=$this->validation->username;?>" tabindex="1"></td>
    </
tr>
   <
tr valign="top"><td align="left">Password</td>
      <
td>:</td>
      <
td><input type="password" class="textfield" name="password" id="password" value="<?=$this->validation->password;?>" tabindex="2"></td>
   </
tr>
   <
tr>
      <
td>&nbsp;</td>
      <
td>&nbsp;</td>
      <
td><input type="submit" class="button" name="action" value="Login"></td>
   </
tr>
</
table>

<?form_close(); ?> 

in model messages:
function output_messages() there is.

in browser :

Fatal error: Call to undefined function output_messages() in /var/www/ci_tes/system/application/views/front/login.php on line 2

if I replace

$this->messages->add($this->validation->error_string'error');
$data['messages']=$this->messages->output_messages(); 

and in view replace

<?=$messages;?> 

it’s “OK”

but if is null $data[‘messages’]
in in my view:

<table width="100%">
    <
tr><td><?=$messages;?> </td></tr>
</
table

in browser

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: messages

Filename: front/login.php

Line Number: 2

why is there..error… ???

thk all

Profile
 
 
Posted: 12 July 2008 10:52 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

@aataqwa—OK, you are really trying.  Please post up the load->view statement from your controller.  And try to explain what it is doing. When you do that, I bet you’ll discover your error…if not, I’ll see if I can help.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 12 July 2008 12:22 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  140
Joined  04-24-2007

Calling output_messages like this in your view should work:

$this->messages->output_messages(); 

or the way I’ve done it, is to create a messages_helper.php, load it in your controller then you can call output_messages in the view:
application/helpers/messages_helper.php

<?php
function output_messages($type null)
{
  $CI 
=& get_instance();
   
  if (
$CI->messages->sum($type) > 0)
  
{
    $messages 
$CI->messages->get($type);
    
// display all messages of the type
    
if (is_array($messages))
    
{
        $output 
'';
        foreach (
$messages as $type => $msgs)
        
{
            
if (count($msgs) > 0)
            
{
                $output 
.= '<div class="' $type '">';
                
$output .= '<ul>';            
                foreach (
$msgs as $message)
                
{
                      $output 
.= $message;
                
}
                $output 
.= '</ul>';
                
$output .= '</div>';
            
}
        }
    }
    
return $output;
  
}
 Signature 

CI Bookmarks

Profile
 
 
Posted: 13 July 2008 10:46 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-08-2008

Thank you very much to all
I am undercommunication in view

<?=output_messages();?> 
should
<?$this->messages->output_messages();?? 

 

thk ontguy.

Profile
 
 
Posted: 19 August 2010 01:45 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  1
Joined  08-19-2010

Works great. A quick search and 1 minute later I have site messages. Thanks!

Profile
 
 
Posted: 02 November 2010 03:50 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  5
Joined  10-27-2010

Thanks for your the work!
I just changed the output method a bit, to have the possibility to get all messages returned as one single string formated with view-files:

// return messages of given type or all types, return false if none, clearing stack
    
function get($mode TRUE$type null// MY MODIFICATION: added $mode (bool)
        
{
        $messages 
$this->_ci->session->userdata('messages');
        if (!empty(
$type)) {
            
if (count($messages[$type]) == 0{
                
return false;
            
}
            
return $messages[$type];
        
}
        
// return false if there actually are no messages in the session
        
$i 0;
        foreach (
$this->_types as $type{
            $i 
+= count($messages[$type]);
        
}
        
if ($i == 0{
            
return false;
        
}

        
// order return by order of type array above
        // i.e. success, error, warning and then informational messages last
        
foreach ($this->_types as $type{
            $return[$type] 
$messages[$type];
        
}
        $this
->clear();
        
        
// MY MODIFICATION START
            //Format the messages to one single Output-String (incl. View-Files)
        
if($mode && $return)
            
{
            $op 
'';
            foreach(
$this->_types as $otp)
                
{
                $op 
.= $this->_ci->load->view("messages/$otp"$return TRUE);
                
}                
            $return 
$op;
            
}
        
//MY MODIFICATION END
        
        
return $return;
        

to use this, you have to add a view-file for each entry which exists in the $_types array. For example:

<?php foreach($error as $message): ?><div id="error"><?=$message;?></div><?php endforeach; ?> 

Now I can use this library perfectly to communicate with the user in a fully templated environment and preserve the ability to adapt the output to the design.

Thanks for this library mate! :D

Profile
 
 
   
 
 
‹‹ URI Language Identifier      Migrations ››