Reputation: 742
I'm working on a custom forum and was wondering what people would consider as the best approach to marking a thread as read.
I want to display a post icon for the threads and if the thread hasn't been read it has an unread icon and the topic title is bold. If its been read then the topic title is normal font weight and the post icon changes to the read icon.
Would the best way be to store in the users session which threads they have viewed and set it up to mark all threads older than 48 hours or so as read?
Thanks!
Upvotes: 2
Views: 480
Reputation: 48304
Create a second table such as:
thread_member: thread_id, member_id, read_flag, posted_flag, bookmarked_flag
Basically anything specific to that member and a thread could go in there. When pulling the list of threads, LEFT JOIN against that table. If read_flag is 0
or NULL
, then it's new.
SELECT * FROM thread t
LEFT JOIN thread_member tm ON t.id=tm.thread_id AND tm.member_id=$member_id
WHERE t.id=$thread_id
Anytime somebody posts a message in the thread, just run this query:
UPDATE thread_member SET read_flag=0 WHERE thread_id=$thread_Id
posted_flag could be used to determine if the person wrote a message in the thread.
bookmarked_flag could be used to determine if the person bookmarked the thread.
To keep the table trim, you could truncate all old records that didn't have any important flags set. In this example, you could delete all records older than X days/weeks/months that don't have bookmark_flag set.
Upvotes: 2
Reputation: 30170
Maintain a table of datetimes a user has last visited a thread
viewed_threads
columns: user_id, thread_id, datetime
Also make sure to keep track of the last time threads were updated
When a user opens a thread
replace into viewed_thread ( user_id, thread_id, datetime ) values( $user_id, $thread_id, now() )
Now when listing threads you can check the user session for the datetime the user last visited that thread. if the last-viewed datetime is before the last time the thread was updated then it's "new"
Upvotes: 2
Reputation: 29160
Off the top of my head, I would keep a separate table that holds the userId, the threadId, and the lastVisted Date/Time. Simply Add/Update the appropriate userId/ThreadId record when the user views a thread.
Then, when a visitor views a thread, you know any posts made after the user's lastVistedDate are new and obviously, if there is no LastVisted date, you know all of the posts in a thread are new.
Upvotes: 2