Search code examples
phpmysqlfacebookcommentsfacebook-feed

Developing Facebook-Style News Feed


I'm really sorry if this question has already been asked, or answered, but I can't quite seem to find what I need.

I have every other piece of this built, my only question is surrounding inline comments. I would like to do something similar to what Facebook does where they render x comments with a button to display all y comments.

However, the only two ways I can see of doing this are:

  1. Performing a SELECT in the loop that renders each item (I think anyone who might have an answer to this would agree with me that this is a terrible decision)
  2. Performing one large select to pull all comments where news_id is in a certain subset, and then use PHP to iterate over them, select the x most recent, and ignore the rest.

Neither one of these seems like a good solution; however, as they both involve a huge waste of resources.

Does anyone have a potential suggestion for implementing this?


Solution

  • SELECT * FROM comments_table WHERE article_id = {something} LIMIT {no_of_comments_per_page} SORT BY date DESC
    

    This is a very simple yet powerful query for the comments.

    Actual code

    <?php
    $sql = "SELECT * FROM comments_table WHERE article_id = 24 LIMIT 40 SORT BY date DESC";
    $data = mysql_query($sql);
    $comments = mysql_fetch_assoc($data);
    foreach($comments as $comment){
      $ct++;
      echo "ID: {$ct}";
      echo "<br />";
      echo "Comment: {$comment["comment"]} by {$comment["user"]}";
      echo "Date: {$comment["date"]}";
    }
    ?>