简体   繁体   中英

How to get newest comments of my friend in social network website

I'm building a social network site like Facebook. Just like some part. I used PHP & MySQL. I've problem with the index page, where I'll show all newest comment action of my friends.

Let me expand the database.

Table comments:

comment_id 
content
datetime
author_id //who created this comment
profile_id //the user_id of the owner profile which this comment commented on.
parent_id //the comment id which is parent of this comment id. if the parent_id = 0 , so this comment is the main comment.

Table users:

user_id
...some user info fields...

Table friendship

user_id // who added another as friend.
friend_id // who was added as friend by another.
status // the status of this friendship, 0 is invited and 1 is alreay friend (accepted)

First, I'll get all my friend id.

$query = "select DISTINCT friend_id as friend from friendship where user_id = ".$user_id." and status = 1
    union
    select DISTINCT user_id as friend from friendship where friend_id = ".$user_id." and status = 1
    ";

Then I'll get all friend id into an array.

foreach($total_friends as $each_friend){
        $all_friend_id[] = $each_friend['friend'];
    }

Now I've a friend ID array. Then I'm trying to get a limited newest comments. I'll get a limited newest comments, whenever it's sub comment or main comment.

"select * from comments where author_id IN ('".implode("','",$all_friend_id)."' order by datetime desc limit 0 , 20";

Now I've a sorted newest sub and main comments array (sorted base in datetime) and if this is a sub comment, I'll get the main comment of this comment, then get all sub comment. If this is a main comment, I'll get all sub comment.

At the end I'll get an array contains all main and sub comments which was sorted as begin. Here's an image demo. http://rongcon.info/demo/abc/newst.PNG

Here will be a duplicate main comment problem when there are many newest sub comment in a main comment. But I can fixed it easy.

I got my goal. But the problem caused when I'm trying to get "older comments" by AJAX.

The problem is in the older comments, sometimes I'll get a sub-comment which in the main comment which was displayed in the first request. So I'll display a duplicate main comment and it's a bug which I need to Fixed!

I'm tried 2 way to fix but I can't handle all bugs.

  1. I'll save all shown main comment ID in JavaScript, then I'll send it into AJAX when I request older comments, In the query of older comment, I'll prevent the main comment and sub comment have it's parent which have ID as the shown main comment ID. So I can prevent this bug.

But When I'm trying to get more and more older comment. The numbers of shown main comment ID will be long and I'm scared that'll a big problem for performance.

  1. I used the logic of show topic have newest reply in forum. I'll add a "last_comment_date" into each main comment. Then I'll get newest comment based only in main comment, not sub comment. So this is going to basic pagination logic and I'll not get duplicate comment when I show older comment.

But I can only get the newest main comment and sub comment in my friend profile and Can't get the main comment which have a newest sub comment of my friend. I'm falling again!

So I'm asking for a best solution for this page.

Instead of selecting all the comments (including subcomments) maybe, select only parent comments first... (where parent_id=0).

select * from comments where parent_id=0 and author_id IN ('".implode("','",$all_friend_id)."' order by datetime desc limit 0 , 20

Then foreach parent, go and get the subcomments, that way when fetching the older comments from ajax, you will not get fragments of subcomments without their parents.

It might be slower though...

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