3

Hello i am trying to make function with while loop in php but cant getting write here is my code

 function mail_detail($mail_detail){

    $data= mysql_query("select * from messages where messages.to = '$mail_detail' and to_viewed = 0 ORDER BY messages.id DESC");
    while ($result= mysql_fetch_array($data)){
    return $result;
    }

}

and out put is

$mail_detail= mail_detail($userid)
echo '<li class="read">

               <a href="#">
                 <span class="message">'. $mail_detail['title'].'</span>
                    <span class="time">
                       January 21, 2012
                   </span>
                                </a>
        </li>';

i am not getting all values just getting one value please help thx

Harinder
  • 1,261
  • 8
  • 27
  • 52

3 Answers3

9

The return statement is terminating your loop and exiting the function.

To get all values, add them to an array in the loop, and then return the array. Like this:

$results = array();

while ($result = mysql_fetch_array($data)) {
    $results[] = $result;   
}

return $results;

on the side that receives the array

$msgArray = mail_detail($mail_detail);

foreach($msgArray as $msg) {
    //use $msg
}

To add on, a function is only able to return once (except for some special circumstances that you should not worry about). Therefore, the first time your function comes across a return statement, it returns the value and exits.

This functionality of return can often be used to your advantage. For example:

function doSomething($code = NULL) 
{
    if ($code === NULL) {
        return false;
    }

    //any code below this comment will only be reached if $code is not null
    // if it is null, the above returns statement will prevent control from reaching 
    // this point

    writeToDb($code);
}
xbonez
  • 40,730
  • 48
  • 157
  • 236
  • i am trying to get all value from that table (messages), if return is terminating what should i use in place of return so this function work with while loop . – Harinder May 23 '12 at 06:14
  • thanks for help but its not working for some reason ... function mail_detail($mail_detail){ $data= mysql_query("select * from messages where messages.to = '$mail_detail' and to_viewed = 0 ORDER BY messages.id DESC" ); $results = array(); while ($result= mysql_fetch_array($data)){ $results[] = $result; } return $results ; } now i m getting no out put ( – Harinder May 23 '12 at 06:27
  • That's because on the other side where you are receiving the output, you are receiving an array. You need to iterate over it. You can't simply output it. Use a `foreach` loop. – xbonez May 23 '12 at 06:30
  • 1
    Thanks Man it worked .... if u can update your mail post it will help others also ... thanks for help – Harinder May 23 '12 at 06:41
  • I'm not sure I get you. What mail post? – xbonez May 23 '12 at 06:42
  • how to out put this with Foreach loop – Harinder May 23 '12 at 06:43
  • Here is what i am using `foreach ($mail_detail as $mail_detail_out) { echo '
  • '. $mail_detail_out['title'].' '. $mail_detail_out['created'].'
  • ';}` – Harinder May 23 '12 at 06:45