[Pugged] Tracking Read Topics/Messages, and the "unread" flag.

Tim Perdue ciapug@ciapug.org
Fri, 6 Sep 2002 13:38:27 -0500


Hi Chris - it's been eons since I've heard anything from you.

> I have been working on a project for a while to write my own forum and I =
have had a bit of a hang up on one area. I was hoping I could get some dire=
ction on this.
> =20
> When the user visits the forum the first time I write a time/date stamp i=
nto his profile for the last visit. Upon their return I grab that time/date=
 stamp create a variable to hold it then update the profile with the curren=
t time/date. While that page is cycling through the forums to display I che=
ck each forum's last post time/date and compare that to the last visit. If =
there has been traffic more recent than the last visit, it gets marked "New=
" to indicate new posts... I do the same with the page which displays each =
of the topic in a forum.
> =20
> However I am getting stuck here; If you are the user and this is a return=
 visit, you are looking at the topic list for a specific forum, and there a=
re 5 new or updated topics since your last visit they are marked with a "Ne=
w" before them in the display. You select the first of the 5 topics, view t=
he messages/posts, and then return to the topic listing, all 5 are now mark=
ed read. Rather, I would like only the topic you just read to be marked rea=
d, and the 4 remaining to still be indicating "New".

I did it this exact same way, and it does have its weak points, but it's
certainly the easiest way to go.
 =20
> What I hoping for in asking is either some pseudo code or a good descript=
ion of how a forum usually handles this. Ask any questions I will do my bes=
t to give you a answer. I will be happy to show the code if needed, let me =
know.

The other way I've seen this done is to create an associative array of each
message you've read and store it in a permanent cookie.

like:

$read_messages[$msg_id]=3Dtrue;
setcookie( foo, blah, serialize($read_messages), bar );

The problem with that is you wind up with an increasingly bloated cookie
getting passed back and forth. I saw this done with the "Phorum" software t=
hat
I used to use on PHPBuilder a while back and the cookie got so big it would
max out your modem transferring it back and forth.

You could solve that by having an algorithm that goes through and whittles
down the array over time. Modern PHP sessions would probably also solve it,
although the session data is lost pretty easily. Not very good for multiple
visits.

You could also create a separate table in the database like:

read_messages table:
--------------------
| user_id | msg_id |
|   5     |   67   |
|   5     |   68   |
|   5     |   61   |
|   5     |   90   |
--------------------

As each message is read, you insert a row into that table for the user.
On reading, you left-join your forum SELECT query against that table, pulli=
ng out
the matching rows for the corresponding user_id.

Probably the easiest and best-performing way is how you have already done i=
t.

Tim

--=20
Founder - PHPBuilder.com / Geocrawler.com / SourceForge
GPG Public Key: http://www.perdue.net/personal/pgp.php
Perdue, Inc. / Immortal LLC
515-554-9520