CodeIgniter Forums
[Solved] Return $data question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: [Solved] Return $data question (/showthread.php?tid=64381)



[Solved] Return $data question - wolfgang1983 - 02-15-2016

When I var dump my my $message variable it displays the multiple messages which is  correct.

But the return only shows one as shown here.

[Image: error_s.png]

How can I produce the same on return


PHP Code:
public function display_error_messages($open_tag '<p>'$close_tag '</p>') {

if (isset(
$this->error_message)) {

 
 foreach($this->error_message as $msg) {

 
   $message $open_tag $msg $close_tag;
 
   var_dump($message);

 
 }


}

return 
$message;





RE: Return $data question - Diederik - 02-15-2016

You recreate (and overwrite) the $message variable instead of appending the second and third messages to it. Try:
PHP Code:
$message .= $open_tag $msg $close_tag



RE: Return $data question - wolfgang1983 - 02-15-2016

(02-15-2016, 02:51 AM)Diederik Wrote: You recreate (and overwrite) the $message variable instead of appending the second and third messages to it. Try:
PHP Code:
$message .= $open_tag $msg $close_tag

I did this now but no getting undefined variable $message

PHP Code:
public function display_error_messages($open_tag '<p>'$close_tag '</p>') {

 
       if (isset($this->error_message)) {
 
           foreach($this->error_message as $msg) {
 
              $message .= $open_tag $msg $close_tag;
 
              var_dump($message);
 
           }


 
       }

 
       return $message;

 
   



RE: Return $data question - Avenirer - 02-15-2016

Define $message='' before entering the foreach loop.


RE: Return $data question - wolfgang1983 - 02-15-2016

(02-15-2016, 03:02 AM)Avenirer Wrote: Define $message='' before entering the foreach loop.

Is This Correct

PHP Code:
public function display_error_messages($open_tag '<p>'$close_tag '</p>') {

 
       if (isset($this->error_message)) {

 
           $message "";

 
           foreach($this->error_message as $msg) {
 
               $message .= $open_tag $msg $close_tag;
 
           }


 
       }

 
       return $message;

 
   



RE: Return $data question - Avenirer - 02-15-2016

if you defined the $message depending on the if statement, then you should also return the $message from inside the if statement...

Code:
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {

        if (isset($this->error_message)) {

            $message = "";

            foreach($this->error_message as $msg) {
                $message .= $open_tag . $msg . $close_tag;
            }
            return $message;


        }
        return FALSE; // or whatever you want to return in case there are no error messages...

        

    } 
But it is you who should tell me if it's correct or not Smile


RE: Return $data question - wolfgang1983 - 02-15-2016

(02-15-2016, 03:27 AM)Avenirer Wrote: if you defined the $message depending on the if statement, then you should also return the $message from inside the if statement...

Code:
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {

        if (isset($this->error_message)) {

            $message = "";

            foreach($this->error_message as $msg) {
                $message .= $open_tag . $msg . $close_tag;
            }
           return $message;


        }
       return FALSE; // or whatever you want to return in case there are no error messages...

        

    } 
But it is you who should tell me if it's correct or not Smile


Thank you for help it all works now. And thank you to every body else to.