Developed by Facebook-Style News Feed

I am very sorry if this question has already been asked or answered, but I cannot find what I need.

I have all the other parts of this build, my only question is inline comments. I would like to do something similar to what Facebook does, where they display x comments using a button to display all y comments.

However, I see only two ways to do this:

  • Performing a SELECT in a loop that displays each element (I think that anyone who could answer this would agree with me that this is a terrible decision)
  • Making one big choice, to pull out all the comments where news_id is in a specific subset, and then use PHP to iterate over them, select the last x value and ignore the rest.

None of these seem like a good solution; however, since both of them involve a huge waste of resources.

Does anyone have a potential suggestion for implementing this?

+4
source share
3 answers
SELECT * FROM comments_table WHERE article_id = {something} LIMIT {no_of_comments_per_page} SORT BY date DESC 

This is a very simple but powerful request for 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"]}"; } ?> 
+1
source

I would use SELECT with the added LIMIT in the sentence, sorting by identifier in DESC order. Something like .. "SELECT * FROM comments LIMIT 3 DESC"

When the user clicks the "load more comments" button, execute some type of AJAX request with a request similar to the request .. "SELECT * FROM comments LIMIT X, 3 DESC"

0
source

I would make a JOIN and sort the data after. Sort of:

 SELECT articles.id, articles.text, articles.date, comments.id, comments.text, comments.date FROM articles LEFT JOIN comments ON (comments.article_id = articles.id) WHERE {some criteria} ORDER BY articles.date, comments.date 

The performance problem with the additional comments that would be hidden is really minor - and you don't have the additional overhead of creating an ajax request to load other comments when they click the "see all" button - you can just hide them and then display them instantly .

If you only need the first 40 posts, you need to add this condition to the where clause (in fact, you could wrap any filtering you do):

 WHERE articles.id IN (SELECT id FROM articles ORDER by articles.date LIMIT 40) 
0
source

Source: https://habr.com/ru/post/1345805/


All Articles