Search code examples
phpmysqlfeed

Show posts from users that I follow, but show my posts too


I`m working on a small website where people can follow each other. There are newsfeed on the site where users can see updates from other users that they follow. The problem is that I can see only posts from other users and not my posts. I want the current user to be able to see his posts too Ordered within the other users posts. Something like Facebook Wall. I have tried tons of differend queries and php, but I get only other users post or when i manage to pull out my posts too there are dublicate post for every user that follows me.

I have 3 tables 'members', 'Post' and 'follow'. The members table hold UserID, Name and Last Name. Post table holds the UserID(FromID in the table) Indexed to members UserID and the post from the user. In the follow table there are 3 columns FollowID, Followed and Follower both Followed and Follower are index to members UserID.

At the moment I use that query:

$query= "SELECT Post.*, members.*, follow.* FROM Post
 INNER JOIN members ON Post.FromID=members.UserID 
 LEFT JOIN follow ON members.UserID=follow.Followed
 WHERE Post.FromID='$user' OR follow.FollowerID='$user'
 ORDER BY Post.date DESC, Post.time DESC";

$user is the UserID of the current logged in user. That query returns all the posts that I want but the problem is that my post are show /n times for each user that follows me.

I really would appreciate the kindness to the one that give me some directions what i do wrong. Thank you


Solution

  • The problem is with your $user. Currently what is happening is that you have an OR condition in where clause. And you are trying to pull either your data or ur followers data.

    My solution for you would be to remove $user from the query you have. So you will have list of followers data only. Have a different query for your posts and then while displaying sort them by datetime Timestamp so you have your posts with other followers too.

    To have your posts and folloeers posts in one query.

    Use union. Like :

    (Query of your posts ORDER BY date) UNION (Query of followers posts ORDER BY date);

    Also check UNION usage in w3schools.

    Hope this helps