如何避免在“无限滚动”中从数据库获取重复帖子系统? [英] How to avoid getting duplicate posts from database in an "infinite scrolling" system?

查看:85
本文介绍了如何避免在“无限滚动”中从数据库获取重复帖子系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX系统,可以从数据库获取帖子,然后在我的布局中创建DIV以放置它们。当te用户到达页面的末尾时,API会加载许多其他帖子。问题是,我如何获得以下10个帖子,这些帖子只是最后一个已经加载的帖子的前一个帖子?当我在顶部加载新帖子时如何显示每个帖子,而不会错过任何帖子或复制任何帖子?

对不起,这个错误的解释和坏的英语。

解决方案

您需要存储上次检索到的邮件的ID。当你提交新的请求时,发送最后收到的消息ID并让php / mysql返回结果。






如果你的查询看起来像这样:

  SELECT * FROM posts WHERE username ='redcurley'LIMIT 10 

code>

然后您需要修改为更类似于:

  SELECT * FROM posts WHERE username ='redcurley'AND id> :id LIMIT 10 

您也可以使用Scott提到的偏移方法。






编辑:我假设您使用自动增量ID,因为它很常见。既然你不是,你需要一些方法来跟踪返回的最后一条消息。您可以通过对记录进行排序来完成此操作,以便每次都以相同的顺序出现并使用偏移量。

I have an AJAX system that fetch posts from database and then create DIVs in my layout to put them. When te user reachs the end of the page then many other posts will be loaded by the API. The problem is, how can I get the following 10 posts that are just the previously posts of the last that is already loaded? The same thing I want to happen when I load the new posts at the top, how to show every post in the right order without missing any post or duplicating any post?

Sorry for the bad explanation and the bad english.

解决方案

You need to store the id of the last retrieved message. When you submit the new request, send the last received message id and have php/mysql return the results from there.


If your query looks something like this:

SELECT * FROM posts WHERE username='redcurley' LIMIT 10

Then you'd need to modify to be something more like:

SELECT * FROM posts WHERE username='redcurley' AND id > :id LIMIT 10

You could alternatively use the offset method that Scott mentions.


Edit: I assumed you were using auto-increment ids because it's so common. Since you aren't, you need some way to keep track of the last message returned. You can do this through sorting the records in a way that they'll come out in the same order every time and use offsets.

这篇关于如何避免在“无限滚动”中从数据库获取重复帖子系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆