-1

I am making a social networking platform. And I just wanted to know of a way to filter out only the post of a user's friends.

Currently, the way I am filtering the posts is by selecting all posts but showing only those posts which are from the user's firend somewhat like this:

$select = $con->query("SELECT * FROM posts ORDER BY timestamp DESC");


while($postassoc = $con->fetch_assoc()){


$postby = $postassoc['user'];
$friendof = $con->query("SELECT id FROM friends WHERE 'from'='$user' AND 'to' ='$postby' OR 'to'='$postby' AND 'from'='$user' AND 'auth'='1'"); 
If($friendof != 0){
//show post
}

}

Assuming there are no errors, this way will take a lot of time on the long run. Is there any better and more efficient method?

Daniyaal Khan
  • 285
  • 1
  • 4
  • 12

2 Answers2

1

Assuming your friend table is like this: table : friends

id user_id   friend_id
1    22        35

Now your post table may be like this table:posts

id  user_id  post_content   post_date
1     35        hbsdhdhs      25-Dec

Now you can use join to filter friend's post

$select = $con->query("SELECT posts.post_content FROM posts join friends on  posts.user_id=friends.friend_id  and friends.user_id =22");
Dinesh
  • 4,066
  • 5
  • 21
  • 35
0

That's extremely inefficient. Are you going to go through every single post on the social networking site to get it? What if you have a billion users?

You'd be better off iterating through all of your friends, pulling the most relevant ones and then displaying their activity. Big social networking sites order your friends in terms of relevance. (you can see this in effect on the 'chat' side panel - your best friends will be up the top.

Once you have a user, the implementation of finding activity is further discussed on this site, e.g. https://stackoverflow.com/a/205477/1752129

Community
  • 1
  • 1
MJT
  • 21
  • 3