tags:

views:

121

answers:

5

I am building a social network site in PHP/MySQL/jQuery. Once a user is logged into my site, I would like to query the DB and get an admin announcement if one exist. This will be a message box that shows on the page to all users but it will have an X to click on it and not show it ever again until the admin post a new announcement message. So if you never click the X and there is announcement messages that exist in the db, it will always show this message box on your page, however if you did cli9ck the X to close the box, then you come back to the page it will not be there, unless there is a new admin message posted.

I know there is several ways of doing this but I am looking for the most efficient way.

One idea I have, if I add an extra mysql field onto the user's table "admin_message" and mark it as 0, then when I post a new admin message it will change the record for every user to 1, if admin message is set to 1 then it shows on user's page. Then when the user clicks the X to hide the box, it will update there user table row and chnage the value back to 0.

Another idea I have is to use cookie's to check if a user has chosen to hide a message, i think this would be faster but maybe not as good, since user can log in with differnt computers and if a new message is shown, they may not see it right away.

So I am just wondering if it is a bad idea to use the extra database field? If I had 1,000,000 users, when I posted a new admin message, then I would need to update 1,000,000 rows to make sure everyone can see the message. Is there a better way? Also once a user logs into my site I will use a session to store the value of them seeing or hiding the message instead of looking at the DB on every page load.



UPDATE

Sorry I think my post might of been a little confusing or not clear on what exactly I meant because most the responses are catered to a message system which this is not anything close to.

Forget the message word please I will try to explain with a different word. Let's say there is 1 admin on the site, that is the only admin who can post a message to users to see. The users will only see 1 message, if there is 2352345234 messages posted over the lifetime of the site, it won't matter, they will only see 1 message, the newest message.

Now some users who log in and see this message "div" on the page might get tired of looking at it, so they will be able to hide it from ever showing again.

It will be as simple as a yes or no for showing this message on there page.

However if I decide I need to post a new admin message for all users to see, then even a user who chose to hide and not show the admin message will still see it again until they choose to never see it again.

A: 

I'd recommend having a admin_message_queue table. It'll have a message body, a user ID, and a message ID. When you post a new admin message it'll add a record for every admin user to that table. Then when they log in you simply select all admin_message_queue rows where user ID = the logged in user.

To get rid of the message you'd just have the close button trigger an AJAX callback to a method on the server that takes the message ID. That method will delete from admin_message_queue where message ID = the one posted in and user ID = the user ID from the session. That way a user can't delete messages for someone else.

Doing it this way saves you from having to keep around rows of viewed messages. Why toggle a bit to hide it for someone? You'll end up keeping around lots of data that's no longer used.

Updated after question update: Sorry I thought you were trying to show messages to just admins. You could keep this same logic for displaying the most recent message to all users, too. Simply have a table with a userID, message text. Whenever you post a message it will go through and overwrite the message text for all people that have records still existing (people who haven't hidden the message) and add rows for other people. When they hide the message delete that user's row.

Parrots
Well honestly it is not that complex. It will be 1 admin on the site and it will only show 1 message, the newest message which will be for every user to see, I just need a way for users to hide the message box if they choose to from showing on there page and a way for me to force hidden box to show if I post a new message. So I don't need to store any user ID's of any sort and I dont need to query for special ID's
jasondavis
A: 

Every user could have a last_message_seen, so you have to query "new" messages (message_id > last_message_seen) for your user. If you [X] close (or timeout), then your javascript can check new messages and so on...

The other idea is to have that last message seen number in the javascript environment, but in that case it will be reseted (recalculated) when you refresh/abandon the page, and if it's not on the DB your user can miss messages inserted between last page load and this refresh.

Or... it can be in the Session, so you don't miss any. When you logon, the number is reseted to some "normal" number, let's say: last message, or message after (now - 1h), or whatever...

helios
+1  A: 

I would use idea #1. I wouldn't use a bool-field to check whether the user has read the message, I'd use a datetime-field, with some default value. If field == default, unread. When read, set field NOW().

That way you know aproximately how fast your users read the message.

EDIT:

after your edit, I'd still use the same mechanism. The message needs a field to find out whether or not is read. If the users clicks the X (to close), update the db and mark the message as read.

If the admin posts a new message, this will popup.

You also need a creation-datetime for your messahe, cause if a user did not close the previous message, and the admin posts a new one, only the latest message can be shown.

EDIT2: In reply to this comment:

Even if a user missed am message, only the newest should be shown. Maybe I am misunderstaning but it sounds like you are saying to basicly mark a message read 100,000 times is 100,000 users click the X and I think it should be more on the user table, show or do not show message box, not on an individual basis

something is not locigal in your theory. You want to save the "show/don't show" as a setting, but you want to show the message anyway. How do you know when to overrule the usersetting and when not without remembering whether the system showed the message to the user? Even if you want to just show it once, you'll need a field in the database (on message-level) to store whether the system showed the message to the user or not.

Natrium
Even if a user missed am message, only the newest should be shown. Maybe I am misunderstaning but it sounds like you are saying to basicly mark a message read 100,000 times is 100,000 users click the X and I think it should be more on the user table, show or do not show message box, not on an individual basis
jasondavis
It's not on a message level, ok to make it simpler to understand, lets pretend there is only 1 message EVER made and I just update that message/comment/notice whatever you like to call it. When I update it then every user should see it on the page even if they choose not to see these. The setting to hide it is just to hide it until I update/post a new one. I think I got it figured out now, I will add a setting field to DB to show or not show homepage notices/message
jasondavis
thanks for trying to understand what I am saying, I know it's hard to explain things sometimes
jasondavis
+1  A: 

You are keeping a login time for the user right? Like a last_login datetime?

So get all messages where date_created >= last_login. Display them, then update the last_login time to now().

+2  A: 

Two simple solutions are as follows:

Good: Check the users cookie, if it contains a flag indicating the message was displayed don't display the message, otherwise display it. When the user closes it, update the cookie. Pros: absolutely simple. Cons: The user might see the same message twice depending on if they clear their cookies or log in from another machine.

Better: Store a flag in the database somewhere (you can store it in the admin user table for now, and down the line break it out into another table). When the user logs in, 1) save this flag to the user's cookie or session, 2) update the database, 3) decide whether the message needs to be shown. When the user closes the message, updare the cookie/session and DB. Pros: User never gets shown the message twice. Cons: slightly more involved as you need to maintain the flag in the db.

Implementation details: For the flag, you could use a message id as suggested already, or more than likely you are already retaining the user's last login timestamp and could use that.