user15063
user15063

Reputation:

Is this a good way of doing what I want (in PHP)?

I've been working on a simple forum for my site. I'm adding a feature that will mark unread posts + forums since your last visit.

Im essentially storing a date of last access to the forum with each user account. When a user comes back to the site, a query is ran to fetch all the post_id and post_parent_forum values, which are then added to an array, that's stored in a SESSION variable with the key being the post_id (which is unique) and the value being the forum id (which wont be unique since posts will appear in the few forums).

In the forum index, I use in_array() for each forum, to see if the forum_id is in the array, if it is, it will be marked as "unread".

In the thread listing, I use array_key_exists() to see if the key for each thread ID is in the array, if it is, its marked as "unread".

When a post is viewed, the item is removed from the array with the key equal to the ID of the thread.

Is this method reasonable, or am I going to run into issues if the forum becomes more popular? Im concerned about running 20 array_key_exists() checks on each forum listing. Is it reasonably fast?

On a side note.... can I work directly with the SESSION stored array, or do I have to assign its value to a regular variable, remove a key, unset the old session var, and re-set it with the updated array?

Upvotes: 2

Views: 136

Answers (3)

Jet
Jet

Reputation: 1171

My practice says: don't worry about hash-tables performance, but think about db-related issues, because they are the most expensive in resources (especially in forum-like projects which are full of text). I never met issues related to PHP performance. MySQL issues always came much faster.

Upvotes: 0

Galen
Galen

Reputation: 30170

Just another route you can go...

Have a last_activity timestamp field in the user table and update it everytime the user loads a new page.

Have a last_updated timestamp field for each post/forum.

To see if a forum/post is "unread" you can just compare the timestamps.

Upvotes: 0

stefs
stefs

Reputation: 18549

php arrays are hashtables! a hash key lookup is not very expensive, so no, i don't think you'll run into performance problems because of this.

in_array is a different matter (searches through the whole array), but really - it still shouldn't be a problem. beware of premature optimization!

can I work directly with the SESSION stored array

yes. $_SESSION is (normally) just an array that is populated when you call session_start() by unserializing the session files content. when the script ends or session_write_close() is called, the $_SESSION array is serialize()'d back to the file. no magic, really.

if you should do that is another question, after all $_SESSION is a global variable (ewwww).

Upvotes: 1

Related Questions