开发 Facebook 风格的动态消息
如果这个问题已经被提出或回答,我真的很抱歉,但我似乎找不到我需要的东西。
我已经构建了所有其他部分,我唯一的问题是围绕内联注释。我想做一些类似于 Facebook 所做的事情,他们用一个按钮呈现 x 条评论,以显示所有 y 条评论。
然而,我能看到的唯一两种方法是:
- 在渲染每个项目的循环中执行 SELECT (我想任何对此有答案的人都会同意我的观点,这是一个糟糕的决定)
- 执行一个大的选择提取 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:
- 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)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个非常简单但功能强大的评论查询。
实际代码
This is a very simple yet powerful query for the comments.
Actual code
我将使用在子句中添加 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"
我会执行
JOIN
,然后对数据进行排序。类似这样的事情:隐藏额外评论的性能问题实际上可以忽略不计 - 并且当他们单击“查看全部”按钮时,您不会产生 ajax 请求来加载其他评论的额外开销 - 您可以隐藏它们,然后立即显示它们。
如果您只想要前 40 个帖子,则必须将此条件添加到 where 子句中(事实上,您也可以包装您正在执行的任何过滤):
I would do a
JOIN
, and sort the data out after. Something like: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):