views:

72

answers:

1

I have written a simple forum in PHP using PostgreSQL. The forum consists of a number of subforums (or categories, if you like) that contain topics. I have a table that stores when was the last time a user visited a topic. It's something like this: user_id, topic_id, timestamp.

I can easily determine what topics should be marked as unread by comparing the timestamp of the last topic reply with the timestamp of the last user visit.

My question is: how do I efficiently determine what subforums (categories) should be marked as unread? All I've come up with is this: every time a user visits a topic, update the visit timestamp and check if all the topics from the current subforum are read or unread. If they are all read, mark the subforum as read for the user. Else, mark it as unread. But I think there must be another way.

Thank you in advance.

A: 

There are many ways (like yours) to achieve a similar behavior, since you mention efficiency I will consider performance is important.

The way I handled this before did not involved a database to take care of unread content at all. That in mind, my suggestion would be:

  1. On the first visit mark only topics newer than, let's say, 3 days as 'unread'
  2. As the user browses the topics, start throwing the topic IDs and LastUpdate for the thread into a cookie on the client
  3. When the forum pages load, check the cookie and if the thread has suffered any updates, this code and also the cookie handling can be easily done with pure javascript.
  4. If the client is a whole week away from the website, no problem, he will see everything newer than 3 days (first visit rule) as unread.

p.s.: This is 100% related to how important is to a person to know what he has not read. In my suggestion this is not something crucial, because it is not 100% reliable (we are not using a database/proper persistance after all)

F.Aquino
This is an interesting approach, but I prefer not to rely on cookies or JavaScript on the client side. Furthermore, as you mentioned, it's not persistent.
bilygates
No prob bily, you can also replicate this behavior in a database, if that helps.
F.Aquino