Search code examples
phpmysqldatabaseforum

custom php forum - showing new/unread posts


I have written myself a custom forum script using php. I decided against using phpbb and others as I wanted 100% flexibility with what I was doing.

I've hit a problem though:

How do I show a user if a post is new/unread or not.

Two solutions come into mind:

1) Cookies 2) Database

I don't want to use cookies because they can be deleted by the user and the firefox side of things means they are auto deleted. Either way, I don't want to use cookies.

The database is causing me a problem because I can't seem to get the database structure sorted in my head! The first solution I could think of was:

  • When a user loads the forums up, check the last time they loaded the forums up
  • Check all the posts have been made since they last viewed the forums
  • Enter those values into the database in a table with fields (user_id, post_id).
  • that value is then deleted from the database when they view the post

The problems I am thinking with this is it's a huge database drain. It seems SO inefficient. I'm sure there are methods with arrays in fields, but I'm not really that good with arrays.

Could anyone give me an indication of a good database design for this as well as any code that has to go with it? It's driving me crazy as I just can't think of a solution that's good and efficient with the server.

Thanks a lot in advance for your help and assistance,

James.


Solution

  • This is somehow a good question, I've never experienced this before so I could only suggest you an idea in which I have no guarantee about its correctness.

    My idea basically is:

    1. Create a new field, called is_new inside a topic table. This field contains a list of values in string form, following a certain pattern. For example: 5|6|12|110|2|45. Each value between | represents a user's ID, who has read the topic.

    2. Every time a user jumps into a forum, while fetching to return the list of topics, you will check if each topic is read by that user, simple by:

      • Explode the string in is_new using explode('|', $row['is_new']);
      • Now you have an array contain the values, just need to check in_array($user['id'], $list_of_ids);
    3. If false, mark the topic unread, otherwise mark it read and also update that user's ID into the is_new list.

    This approach seems less 'painful' than your original approach, since it only checks further more than ordinary, and as simultaneously as you fetch the list of topics. I think it will affect a little bit more if you have heaps of users.

    Note: you don't have to worry about the problem of normalization since is_new contains multiple values, you only use it to check and update, no selection required.

    In addition, alternatively, you can check the topics are new or not using time comparison. For example, if a topic lasts for 4 weeks and a user doesn't read it, then it returns old even though it's unread (it makes sense doesn't it? Just like a newspaper). I think you will be able to do this, quite easy indeed.

    This approach is particularly applied by quite a few forum software, since it is quick and makes more sense. Remember that the browsers initially support you with determining read/unread topics, i.e. the color of hyperlink to the topic will be changed if it's visited. Now it turns back to the Cookies solution. I wouldn't worry about users deleting their cookies. This happens rarely and the users won't die just because the read topics turn unread after they delete the cookies.

    Quite an open-ended topic isn't it? Hope this helps (: