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

3 Answers 3

11

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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

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 .
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 (
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.
Thanks Man it worked .... if u can update your mail post it will help others also ... thanks for help
I'm not sure I get you. What mail post?
|
0
function mail_detail($mail_detail){
    $returnArr = array();
    $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)){
        $returnArr[] = $result;
    }
    return $returnArr;

}

This way you return everything returned because you push it in one array and as your loop finishes, the whole array wil be returned. Because just like xbones said, a return breaks your loop!

Comments

0

harinder,Function(mysql_fetch_array($data)) return an array. That means your $result is an array, so when you recevie the $result at view page you have to extract it using foreach look like this:

foreach($result as $item)
 {
   echo $item->user(<-here you have to write the column name ,that you want to retrieve)
 } 

Hence you can get your all results in array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.