开发 Facebook 风格的动态消息

发布于 2024-10-27 13:14:29 字数 368 浏览 1 评论 0原文

如果这个问题已经被提出或回答,我真的很抱歉,但我似乎找不到我需要的东西。

我已经构建了所有其他部分,我唯一的问题是围绕内联注释。我想做一些类似于 Facebook 所做的事情,他们用一个按钮呈现 x 条评论,以显示所有 y 条评论。

然而,我能看到的唯一两种方法是:

  1. 在渲染每个项目的循环中执行 SELECT (我想任何对此有答案的人都会同意我的观点,这是一个糟糕的决定)
  2. 执行一个大的选择提取 news_id 位于某个子集中的所有评论,然后使用 PHP 迭代它们,选择最近的 x,并忽略其余的。

这些似乎都不是一个好的解决方案。然而,因为它们都涉及巨大的资源浪费。

有人对实施这个有潜在的建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩物 2024-11-03 13:14:29
SELECT * FROM comments_table WHERE article_id = {something} LIMIT {no_of_comments_per_page} SORT BY date DESC

这是一个非常简单但功能强大的评论查询。

实际代码

<?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"]}";
}
?>
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"]}";
}
?>
春庭雪 2024-11-03 13:14:29

我将使用在子句中添加 LIMIT 的 SELECT,按 DESC 顺序按“id”排序。类似于..“SELECT * FROM comments LIMIT 3 DESC”

当用户单击“加载更多评论”按钮时,执行某种类型的AJAX请求,查询类似于..“SELECT * FROM comments LIMIT X, 3 DESC”

I'd use a SELECT with a LIMIT added to the clause, ordering by 'id' in DESC order. Something like.. "SELECT * FROM comments LIMIT 3 DESC"

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

剪不断理还乱 2024-11-03 13:14:29

我会执行JOIN,然后对数据进行排序。类似这样的事情:

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

隐藏额外评论的性能问题实际上可以忽略不计 - 并且当他们单击“查看全部”按钮时,您不会产生 ajax 请求来加载其他评论的额外开销 - 您可以隐藏它们,然后立即显示它们。

如果您只想要前 40 个帖子,则必须将此条件添加到 where 子句中(事实上,您也可以包装您正在执行的任何过滤):

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

I would do a JOIN, and sort the data out after. Something like:

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 issue of having extra comments that would be hidden is really negligible - and you don't have the added overhead of making an ajax request to load the other comments when they click that 'see all' button - you can just hide them, and then display them instantly.

If you wanted only the top 40 posts, you would have to add this condition to the where clause (in fact, you could wrap whatever filtering you're doing in as well):

WHERE articles.id IN (SELECT id FROM articles ORDER by articles.date LIMIT 40)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文