简体   繁体   中英

PHP MYSQL Nested While loop skips iterations

Need help to get through this Nested WHILE loop.

The Scenario is

These are the 5 tables in my db

user_info(userid,name,picurl)
user_friends(userid,friend_id)
user_post(userid,post_id,post,time)
user_likes(userid,post_id)
user_comments(userid,post_id)

I want to access user_post table and populate the data in my android application. i can access userid,post_id and time from user_post table and using friend_id of the user from friends table. Now to display a complete post with pic and name i need to access name and picurl of the person who's post it is from user_info table. If i need to display one 1 friends post i can do this simply but when there are couple of friends with more than 1 post i can't get the name and picurl of the user and it displays null in json response. I'm using Nested While loop for this operation following is my code.

$userid = $_POST['userid'];

$query = "SELECT * FROM user_friend WHERE userid = '$userid'";
$result = mysql_query($query);

$return_arr = array();
$return_arr['newsfeed'] = array();

//Access userid
while($row = mysql_fetch_assoc($result))    {

echo "Friend Name is ".$row['friend_id']. "<br>";       

$friend_id = $row['friend_id'];
$friend_id = mysql_real_escape_string($friend_id);

//accessing user_info table to access user info
$picquery = "SELECT * FROM user_info WHERE userid = '$friend_id'";
$picresult = mysql_query($picquery);

$pic = mysql_fetch_assoc($picresult);

$row_array['name'] = $pic['name'];
$row_array['picurl'] = $pic['picurl'];

$query2 = "SELECT * FROM user_post WHERE userid = '$friend_id'";
$result2 = mysql_query($query2);

//Access Posts Against userids
    while( $row = mysql_fetch_array($result2) ) {

                $post_id = $row['post_id'];

                //for number of likes
                $likesQuery = "SELECT COUNT(*) as totallikes FROM post_likes        WHERE post_id = '$post_id'";
                $likesResult = mysql_query($likesQuery);
                $likesvalues = mysql_fetch_assoc($likesResult); 
                $num_of_likes = $likesvalues['totallikes']; 

                //for number of comments
                $commentsQuery = "SELECT COUNT(*) as totalcomments FROM post_comments WHERE post_id = '$post_id'";
                $commentsResult = mysql_query($commentsQuery);
                $commentsvalues = mysql_fetch_assoc($commentsResult); 
                $num_of_comments = $commentsvalues['totalcomments'];

        $row_array['post_id'] = $row['post_id'];
        $row_array['userid'] = $row['userid'];
        $row_array['post_text'] = $row['post_text'];
        $row_array['post_time'] = $row['post_time'];
        $row_array['post_num_likes'] = $num_of_likes;
        $row_array['post_num_comments'] = $num_of_comments;

        array_push($return_arr['newsfeed'],$row_array);

    }
}
date_default_timezone_set('Asia/Karachi');
$date = date(' h:i:s a d/m/Y', time());         
echo json_encode($return_arr,JSON_UNESCAPED_SLASHES);

something like this could help. As I don't have any test data I cannot see how it's working. It should display the post_id along with the count of the likes and the comments

SELECT
  p.post_id,
  COUNT(c.post_id) AS comments_count,
  COUNT(l.post_id) AS like_count
FROM user_post p,
     user_likes l,
     user_comments c
WHERE p.post_id = l.post_id
AND p.post_id = c.post
GROUP BY p.post_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM