Nested While Loop for mysql_fetch_array not working as I expected
Posted
by
Ayush Saraf
on Stack Overflow
See other posts from Stack Overflow
or by Ayush Saraf
Published on 2011-11-13T09:42:32Z
Indexed on
2011/11/13
9:50 UTC
Read the original article
Hit count: 412
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 tht 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 wht 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 loopo insted of while loop that mite wrk...
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>";
}
}
}
© Stack Overflow or respective owner