Marking posts as read - how is it done?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Marking posts as read - how is it done?

Post by RFairey »

How do forum systems such as phpBB mark posts as having been read by any particular user? By this I mean how do they keep track of when to tell a user there are new posts since their last visit?

I am building a simple forum, I have posts stored in a mySQL database, each with a timestamp. User logins are also recorded, with a second timestamp. It is not sufficient to merely compare the post time and login time, since as soon as a user posts, his own post gets marked as being new. Also, a user may not necessarily view a post that is new, and this should remain 'unread' as it were across any number of logins until it is viewed.

What sort of data structure is used to keep track of all this? Is it feasible to store an array of users in the info of a post or vice versa?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could use serialize/unserialize...

Code: Select all

// set some values
$array[] = 'foo';
$array[] = 'some string goes here';
$arrayX[] = 'bar';
$arrayX[] = 'kittythrow';
$arrayX[] = $array;

// see what you have done...
print_r($arrayX);

// serialize the data, show result
$foo = serialize($arrayX);
echo $foo."\n";

// un-serialize the data, show result
$foo = unserialize($foo);
echo $foo."\n";

// oh my, it's an array, lets make that better looking...
print_r($foo);
You could use the serialized value in a cookie for example, because such string are easier stored. Then read the cookie, and unserialize it, to restore the original values...
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

aye as jam said, the best way is normally to store an array (serialised or not) in a cookie.

your array would probably be something like
$thingy[#thread_number#] => $time_visited
or on a larger site you would use the forum number rather than the thread number
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Would this work to put a php array into a mysql database? Put values into array -> serialize -> stick serialized data into mysql table -> read from table -> unserialize -> use?

I am thinking more along the lines of an array for each post containing the users that have read it.
Post Reply