I have a MySQL database with thousand of articles. Each article can be flagged as READ or UNREAD (bit). In a web interface a user can choose how many articles to display and whether to view all of them, or just the UNREAD ones. In Javascript side I store a limitStart value that I use to mount the LIMIT for the next query. For example in the first call we get the first 3 articles, I store limitStart=3 and in the next call I ask for more starting from 3.
In any moment the user can mark an article as READ, and here is the problem. If a user has the UNREAD filter on, then marks an article as READ and asks for more items, he will miss an article because the limitStart is not anymore 3 (but 2) because he has actually mark one as read.
Imagine we have this list of articles in the database, flagged as follows:
1 UNREAD
2 READ
3 UNREAD
4 READ
5 UNREAD
6 UNREAD
7 UNREAD
8 UNREAD
9 UNREAD
In the first call with the UNREAD filter on, a user gets the articles 1,3,5 (SELECT * WHERE UNREAD=1 LIMIT 0,3
).
Now he marks article 3 as READ and asks for more with: SELECT * WHERE UNREAD=1 LIMIT 3,3
1 UNREAD
2 READ
3 *READ
4 READ
5 UNREAD
6 UNREAD
7 UNREAD
8 UNREAD
9 UNREAD
He gets articles: 7,8,9. He missed article 6!!
I can not figure out how to solve this, apparently, simple problem.
I have already tried to:
- Decrement the counter: no way because I have more flags with more states (like favs.) and this behavior happens with all of them.
- Use a timestamp and start from that timestamp: failed because items can have the same timestamp.
Thanks for your time.