Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    Change Link Color If Message is Unread

    I made a very simply message board that allows registered users to post comments.

    How can I make the link to the message board change(ex: from blue to green) whenever there is a new unread message? any tips? I am not sure where to start!

  • #2
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Perhaps have a parallel table containing a bunch of bool variables for each user/thread?

    it would take a bit to nut it out but i think that would work...

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by srule_ View Post
    I made a very simply message board that allows registered users to post comments.

    How can I make the link to the message board change(ex: from blue to green) whenever there is a new unread message? any tips? I am not sure where to start!
    for current user session you can use css, a:visited, like this:

    Code:
    a { color: #00f; }
    a:visited { color: #0f0; }
    but this don't work if the user logout and clean the cache.
    also when the user login all the message are by default unreaded, maybe
    it work if you can find a solution to mark the readed message from previous
    user session and at login time to change a element style to readed.
    Maybe onclick, onkeypress with javascript could help.

    Smell like ajax, .

    PS: I know that this is not a php solution,

    best regards
    Last edited by oesxyl; 01-10-2008 at 03:50 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    PS: I know that this is not a php solution,
    I got the idea to change the link color from another persons site who has a simple message bord similar to mine. At first i thought he used Javascript but I check his source code and there was none!


    The Private Messages on this site is similar to what I want as it shows my unread messages untill I click the link to view them. This is similar to what I need. Do you know what it was done on this site?
    Last edited by srule_; 01-10-2008 at 04:12 AM.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by srule_ View Post
    I got the idea to change the link color from another persons site who has a simple message bord similar to mine. At first i thought he used Javascript but I check his source code and there was none!


    The Private Messages on this site is similar to what I want as it shows my unread messages untill I click the link to view them. This is similar to what I need. Do you know what it was done on this site?
    I can imagine, but I don't know,

    You probably use a session mechanism so you can store in mysql into a table something as:

    user_id, message_id, flag

    flag is the state of the message with a specified message_id for a user identified by user_id.

    all the stuff is around this table, insert when you have message for a user , update when you change the state of a message and select to output on the page.

    I suppose the 'secret' part is when the user click on the link,

    IMO is simple:
    1. click => redirect to a script passing the user_id and other parameters you need later.
    2. the script change the flag to readed
    3. redirect again using referer this time to the message

    From my previous expericence with you from other thread I think that you can implement that without problems,

    best regards

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    In general here is how a forum/message board would handle un-read messages -

    Your database needs a column in the "member" table to hold a "last visit" datetime.

    Any time a member arrives at the site, retrieve the previous "last visit" datetime and add rows to a table (one table) of "unread" threads using the member id and the thread id's of all the threads that have a datetime that is newer than the "last visit" datetime (I would store the datetime of the thread in this table as well so that cleaning out old un-read entries is easy) and then update the "last visit" datetime for that member to the current datetime. If the person leaves the site and comes back or refreshes the page, this process is repeated to add any newer threads to the unread table. This will make a table of unread threads that will contain rows for any "active" member that visits the site.

    If someone has not visited the site for a long time and then does visit, you might want to limit how far back in time you check the datetime on threads. If their "last visit" datetime is farther back in time than say the current datetime - 2 months, then just set the "last visit" datetime to that value before you get the threads that are newer than that.

    You can limit and clean out old unread thread id's if you want. For example, if someone visits the site after 2 months, clean out any un-read threads older than 2 months at the same time you add any new un-read threads to the unread table.

    Also, if a member id is deleted, any rows in the unread table for his id should be removed.

    When you display threads for any member, use his id to check the unread table and do the color change if there is a row in the unread table.

    When they read a thread, delete the row from the unread table that matches their id and the thread id. If they re-read a thread the row will already have been deleted, so a delete operation will not match any rows.

    Short version - Use a "last visit" datetime in your member table to act as a "bookmark". Any time a member visits/refreshes, use their "last visit" datetime to add any threads newer than that to the "unread" table and update their "last visit" datetime to the current datetime. When displaying threads, check if there is a row for that member and the thread in the "unread" table to control the color that is displayed. When the member reads a thread, remove the row with their id and the thread id from the "unread" table. Set some reasonable limits on how far back to add threads to the "unread" table when a member has not visited the site for a while and for how long (using the datetime of the thread) rows will be left in the "unread" table.

    Edit: I forgot this part - To have a "Mark all messages as read" button, simply set the "last visit" datetime to be the current datetime and delete any rows for that member id from the "unread" table.
    Last edited by CFMaBiSmAd; 01-10-2008 at 05:13 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    srule_ (01-10-2008)

  • #7
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Thank you for that detailed response! I'm pretty sure I can figure the code out now that u have explained what it should do :-)

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Hmm ok, my users "last_seen" field is a UNIX TimeStamp, and my "Post_Time" field uses the the MySql TimeStamp. Will this be a problem when comparing the times?

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by srule_ View Post
    Hmm ok, my users "last_seen" field is a UNIX TimeStamp, and my "Post_Time" field uses the the MySql TimeStamp. Will this be a problem when comparing the times?
    no, it help because unix time stamp is seconds since 1 Jan 1970, so you compare and operate with integers.
    the only 'problem', not real, will be conversion to/from date representation.

    best regards

  • #10
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    Any time a member arrives at the site, retrieve the previous "last visit" datetime and add rows to a table (one table) of "unread" threads using the member id and the thread id's of all the threads that have a datetime that is newer than the "last visit" datetime (I would store the datetime of the thread in this table as well so that cleaning out old un-read entries is easy) and then update the "last visit" datetime for that member to the current datetime.
    Sorry for bumping the topic, but it's better than creating a new topic, isn't it?

    I was just wondering if anyone had an example of a piece of code for the above statement.
    Thank you for your time to read my post

  • #11
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Sylvester21 View Post
    Sorry for bumping the topic, but it's better than creating a new topic, isn't it?

    I was just wondering if anyone had an example of a piece of code for the above statement.
    Anyone?
    Thank you for your time to read my post


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •