How can I show users that they have a new message or response awaiting in their inbox by showing them a number or text telling them to check it and then only remove the notification automatically if the user responds to it or clicks "done" (meaning no further response necessary).
I assume the messages will be stored in a DB. Just add a 'read' boolean column and then query the DB for unread messages to know whether there are new messages.
When the user replies you set the read column to 1.
This really needs javascript. Basically you have javascript make a request every x amount of seconds to a php page that checks for new notifications. Then the javascript alerts the user.
if you wan't something like on facebook, and other social network sites, U need to use javascript to do this. Php is serverside language, and that can;t do.
if you don't ask that, and u are only interested how to solve this with php and database.
Try to make new row. Called status, and update table set that coloumn is 1 if is read, 0 if isn't read...
Related
I have a simple and generic internal messaging service on one of my PHP/MySQL sites, with a unique messageID, from, to, subject, message, time, and isRead.
In general, what is the best way to check that a user has new mail?
Should I store a simple binary trigger on the main Users table that gets switched to 1 when anyone sends them a message, and then have the user check for that 1 on every pageload, and if there is one, alert them to the new mail (and set it to 0 whenever they go to their Inbox)? But what if it was spam and we deleted it before the user read it?
Or should I store the messageid of the last message the user read, and then do a check for the latest message and see if it's more recent than the last one that he read, and if so alert him? But then how and where should I store this info?
Is there another, more efficient method considering we would have to check for new messages on every pageload?
If the user goes to his Inbox, it should no longer show him that he has "New Mail" for any of the messages that were in his Inbox at the time he checked it, regardless of whether he's actually clicked to read them or not.
Thanks!
EDIT: Some of my users access my site from very basic phones that don't have cookies or even Javascript, so please keep that in mind.
Best way would be push notification from server, like stackoverflow does, using html sockets.
jquery plugin
But keep in mind that is not supported by all browser, so will need to fall back to ajax polling.
About spam i would suggest only notify user after spam checking if done, if possible.
Your solution to store next to user with set bit sounds right, (also you could store number of new message, instead of bit)
In your users table, store the count of new messages, and update when needed.
AJAX polling can be expensive in serverside (the select count(*) from ... can be expensive when your DB became large, and you need to do it, for example, one check per minute).
If the user just browsing in your website, and have no new messages, you can just skip select more information about messages.
I've made a fairly basic AJAX chat with PHP & MySQL. The chat messages are stored in the MySQL database, and I poll (I know, I know) every few seconds to check for new messages.
In order to keep the bandwidth and update times down, I've set it up so that on the first request it returns all of the messages, and then each request after that only returns messages with an id greater than the last message id received by the client, adding these new messages to the bottom of the chat and removing old ones at the top to keep the chat at a set 150 messages at all times.
This works great, but there is one fatal flaw. When a chat message is deleted by a moderator, it won't get removed from the chat screen unless you reload the page. How can I either alter my system to allow for messages to be deleted, or change my approach so that this will work?
You could keep track of the deleted ids somehow. Either just flag them deleted in the database, or if you have to truly delete the record, keep track of the IDs of those deleted messages. Then add a new section to your AJAX response that lists those deleted IDs.
The clients could then go through the message history and delete the corresponding entries. Assuming you've been attaching the message IDs to the messages in the chat history, this should be a relatively trivial operation.
I currently have a messaging system.
I am looking to make it so that when a user receieves a new message, they get an alert. A popup perhaps? Like a message box saying "you have a new message".
How could I achieve this?
Thanks
You can't really do this in php, as normal using of php implies no direct user interaction. What you need to do is to implement the relevant user interaction code in for example javascript, and do polling towards the server hosting the php (using AJAX or similar), to see if an popup should be shown or not.
This is not only a php problem. You need to make a database table like this:
messageID
contents
...
popuped
userID
Then make a page who gets the count of messages who are not popuped already:
SELECT COUNT(*) AS nr FROM messages WHERE userID = :userID AND popuped = 0
And after that set popuped to 1:
UPDATE messages SET popuped = 1 WHERE userID = :userID
And print the nr field.
Then for every page on your site you need to make a scheduled ajax request (for example every 20 seconds) who loadeds that page. If the result is higher as zero you need to show that the user has messages.
For the ajax request take a look for example at the mootools javascript framework (or jQuery, Prototype, etc.)
Are you looking for a 'live' message, as in an alert displays while the user is doing other things? Or are you looking for a 'static' message, as in an alert that displays when the user opens a page? The static method can be achieved with server-side php. The live method will need to employ client side techniques. I recommend using jQuery and jGrowl and something like jQuery Timers for the live methods. Try this SO question about javascript timers here
im using comet to push new data to the user.
1: but i wonder how i should check if new data (new messages, new replies etc) is available?
should i in the php in the background use a while loop and sleep it for 1 min so it can check every one min if new data has come in?
or should i have a trigger in the database for this?
what available options are there?
2: and how can i actually check eg. if user got a new message with SQL? how should i set up "the system"?
1) I would use AJAX to ask for new messages from an opened user page. This way you do not need a script running on the background and only active users/pages need the results, so let them ask for it themselves.
2) If you send the date of the last message along with the AJAX request. It is fairly easy to check if the database contains newer messages with something like SELECT * FROM messages WHERE date > $last_message_date
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.
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.
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...
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().
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.
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.