Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I m trying to make feed system for my website, in which i have got i data from the database using mysql_fetch_array system and created a while loop for mysql_fetch_array and inside that loop i have repeated the same thing again another while loop, but the problem is that the nested while loop only execute once and dont repeat the rows.... here is the code i have used some function and OOP but u will get what is the point! i thought of an alternative but not yet tried vaz i wanna the way out this way only i.e. using a for loop instead of while loop that might work...

public function get_feeds_from_my_friends(){
    global $Friends;
    global $User;
    $friend_list = $Friends->create_friend_list_ids();
    $sql = "SELECT feeds.id, feeds.user_id, feeds.post, feeds.date FROM feeds WHERE feeds.user_id IN (". $friend_list. ") ORDER BY feeds.id DESC";
    $result = mysql_query($sql);
    while ($rows = mysql_fetch_array($result)) {
            $id = $rows['user_id'];
            $dp = $User->user_detail_by_id($id, "dp");
            $user_name = $User->full_name_by_id($id);
            $post_id = $rows['id'];

            $final_result = "<div class=\"sharedItem\">";
            $final_result .= "<div class=\"item\">";
            $final_result .= "<div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>";
            $final_result .= "<div class=\"txtHolder\">";
            $final_result .= "<div class=\"username\"> " . $user_name  . "</div>";
            $final_result .= "<div class=\"userfeed\"> " . $rows['post'] . "</div>";
            $final_result .= "<div class=\"details\">" . $rows['date'] . "</div>";
            $final_result .= "</div></div>";
            $final_result .= $this->get_comments_for_feed($post_id);
            $final_result .= "</div>";

            echo $final_result;
        }
}

public function get_comments_for_feed($feed_id){
    global $User;

    $sql = "SELECT feeds.comments FROM feeds WHERE feeds.id = " . $feed_id . " LIMIT 1";
    $result = mysql_query($sql);
    $result = mysql_fetch_array($result); 
    $result = $result['comments'];
    $comment_list = get_array_string_from_feild($result);

    $sql = "SELECT comment_feed.user_id, comment_feed.comment, comment_feed.date FROM comment_feed ";
    $sql .= "WHERE comment_feed.id IN(" . $comment_list . ") ORDER BY comment_feed.date DESC";
    $result = mysql_query($sql);
    if(empty($result)){ return "";}
    else {
        while ($rows = mysql_fetch_array($result)) {
                $id = $rows['user_id'];
                $dp = $User->user_detail_by_id($id, "dp");
                $user_name = $User->full_name_by_id($id);
                return "<div class=\"comments\"><div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>
                            <div class=\"txtHolder\">
                                <div class=\"username\"> " . $user_name  . "</div>
                                <div class=\"userfeed\"> " . $rows['comment'] . "</div>
                                <div class=\"details\">" . $rows['date'] . "</div>
                            </div></div>";

        }       

    }
}
share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

It's because you're using return, which stops the function from further executing. What you're trying to do is

//...
else {
    $out = '';
    while ($rows = mysql_fetch_array($result)) {
            $id = $rows['user_id'];
            $dp = $User->user_detail_by_id($id, "dp");
            $user_name = $User->full_name_by_id($id);
            $out .= "<div class=\"comments\"><div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>
                        <div class=\"txtHolder\">
                            <div class=\"username\"> " . $user_name  . "</div>
                            <div class=\"userfeed\"> " . $rows['comment'] . "</div>
                            <div class=\"details\">" . $rows['date'] . "</div>
                        </div></div>";

    }
    return $out;

}
share|improve this answer
 
okay i m goona try this out in a min –  Ayush Saraf Nov 13 '11 at 10:01
 
i got your point but tht loop alone is also not working is there any error that u can find? –  Ayush Saraf Nov 13 '11 at 10:09
 
thanks a lot u was correct –  Ayush Saraf Nov 13 '11 at 10:25
add comment

your inner loop returns within the first iteration of the loop. Are you sure you should be looping? If you're certain it's always going to be the first row, why not just grab the first row?

share|improve this answer
 
see its a different funciton for the comments there are many abt 3 comments for 1 of the post... i want all of them to be echo the query in get_comments_for_feed is giving 3 rows if i run it in mysql console so the plob is tht while loop doesn't loopo it do tht once and move on to the main loop and then the $post_id changes so i need tht loop to work –  Ayush Saraf Nov 13 '11 at 9:54
add comment

Never use a nested while loop, it will not give the result you expected. Do it the following way, as I did in this script:

<?php 
include('dbconnect.php');

$query = "SELECT * FROM tiles_categories" . " LIMIT 0, 3" ;
$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result)) {  $category_id = $row["cat_id"] ;
echo "<div class='product'>";
            echo "<h2 style='padding-bottom:10px; color:#6F0D00;'>". $row['cat_name'] . "</h2>";
            gettiles($category_id);
        echo"</div>";
    }

    function gettiles($category_id) {
        $tiles_query = "SELECT * FROM tiles_info where cat_id = '".$category_id."'";
        $result = mysql_query($tiles_query) or die(mysql_error());
        while($row_tiles = mysql_fetch_array($result)) {
            echo "<div align='center' style='float:left; width:100px; margin:0px 35px 0px 0px;'>";

            echo "</div>";
        }
    }
?>
share|improve this answer
 
Please use mysqli or PDO instead of mysql. –  Mansfield Oct 29 '12 at 12:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.