Lostnode Posted October 2, 2012 Share Posted October 2, 2012 I am creating a multi-user system and want to broadcast system updates to a dashboard (when they log in they see the new messages). The user can then close the message, making it disappear from the screen without refreshing. I know that this would have to use ajax and jquery, but for the life of me I cannot find anything on the net to help me. I can find stuff on pop up notifications but its not quite what i am looking for. I am looking at it pulling a list of notifications/updates from a mysql database and after the user reads it, its gone (but they can click an archives button and read them all in case they accidentally clicked one off) but I am looking for a more efficient way of doing it then having an entry for every user put into the database after they have read it do it doesn't pop up again next time they refresh the page. Is this possible or so I absolutely have to make an entry for each? Also if anyone has any tutorials on the ajax/jquery functions I need to make this work it would be much appreciated. Also, just to add the the mix, I am creating it to be a desktop application with EXEOutput, would it be easier/possible to make it so that if I do need to do an entry per each user, it put the entry into the SQLite flat file db on the local system and not the mysql server? So that it pulls the data from the MySQL DB on the main server and cross references those already viewed by pulling from the local SQLite DB? Just looking for some hints on how to accomplish this. LostNode. Quote Link to comment https://forums.phpfreaks.com/topic/268982-creating-dashboard-messages-seeking-best-practices/ Share on other sites More sharing options...
spiderwell Posted October 2, 2012 Share Posted October 2, 2012 whilst reading this, i thought one way to approach it is:when a user logs in, the message system returns all messages since previous login, and this will bypass having to save many messages per user table crap. This does of course rely on your login system recording times/dates for logins as for ajax jquery, does your login use it? if so, can you not adapt that? closing a div wont require ajax, you are just changing its display to none essentially. Quote Link to comment https://forums.phpfreaks.com/topic/268982-creating-dashboard-messages-seeking-best-practices/#findComment-1382225 Share on other sites More sharing options...
Psycho Posted October 2, 2012 Share Posted October 2, 2012 (edited) I'm in agreement with Spiderwell. From the logic you are wanting, it doesn't seem that there is really any specific "read" event other than the message was displayed to the user. And since you want to display new notifications upon login you don't need to track which notifications have been read - you only need to display the notifications created since the user's last login. Also, no need to muddy the waters with AJAX - at least initially. Build the core functionality first. Then, you can modify using AJAX later if needed. But, in this case - as Spideerwell stated - you may not even need AJAX. Just populate the notifications into a DIV and provide a button to hide/remove the notifications. That can all take place on the client-side. Now, the one trick to this is that when a user logs in you need to store their previous last login (such as in a session variable) before updating their record with the current login datetime. Otherwise, if you were to use the last login time from their user record it would always be after all the notifications. Here is a rough outline of some code to get you started <?php //Get any new notifications created after prev login $query = "SELECT date_created, notification FROM notifications WHERE date_created > {$_SESSION['prev_login']}"; $result = mysql_query($query); //Create notifications output $notifications = ''; if(mysql_num_rows($result)) { //Opening DIV container $notifications = "<div id='notifications'>"; while($row = mysql_fetch_assoc($result)) { //Individual notification DIV $notifications .= "<div>{$row['date_created']}: $row['notification']<div>"; } //Link to close/hide notification DIV $notifications .= "<a href='#' onclick=\"document.getElementById('notifications').style.display='none'\">Close</a>"; //Closing DIV container $notifications .= "</div>"; } ?> <html> <body> <?php echo $notifications; ?> </body> </html> Edited October 2, 2012 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/268982-creating-dashboard-messages-seeking-best-practices/#findComment-1382292 Share on other sites More sharing options...
Lostnode Posted October 2, 2012 Author Share Posted October 2, 2012 Thank you both for the ideas. I never thought of doing it that way, and it is much simpler than the track I was on. I will figure out a way to do it as there will be stand alone apps which simply check is a key is valid before the software lets them in, I will just have the software mark the last time the key was checked and store it in memory. Quote Link to comment https://forums.phpfreaks.com/topic/268982-creating-dashboard-messages-seeking-best-practices/#findComment-1382302 Share on other sites More sharing options...
Adam Posted October 3, 2012 Share Posted October 3, 2012 (edited) The only problem I can think of with the approach mentioned by spdierwell, is people not actually noticing or taking the time to read the notification on the first view of the dashboard. It seems to me like a lot of the notifications would go unread? Perhaps a better approach would be to have a many-to-many table (called `unread_notifications` let's say) that stores the user's ID along with a notification ID. When the user logs in, check their last login timestamp and insert any notification IDs published since that point and within a valid time range or limit, into `unread_notifications` for that user. When the user clicks "I've read them all", or individually marks them as read, you just delete from `unread_notifications`. You can set an expiry date, or infer the expiry date from the publish date, to clean up any old notifications that aren't relevant any more. Edit: This way you're only inserting a limited number of notifications into the table, you're only doing that for active users, and you're clearing up after yourself to ensure the table doesn't bloat out of proportion. The action to mark them as solved doesn't need to be done with AJAX, just make it a link to start with that redirects back to the dashboard. You can then add in the AJAX request afterwards. Edited October 3, 2012 by Adam Quote Link to comment https://forums.phpfreaks.com/topic/268982-creating-dashboard-messages-seeking-best-practices/#findComment-1382467 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.