MySQL AJAX PHP Notification Table Layout - php

I am having issues finishing off my notification system on my website, where a user is notified when a post or comment is made. So far, I have 3 tables: users, posts, and notifications. When a user submits content to the posts table, for example, a notification will be generated in the notifications table including the timestamp, where the post was, and who did it. My question is not on coding any of this, but how my tables should be set up so that when i call the query via AJAX, the user gets a number representing the number of posts/comments they have NOT seen. How do I represent in tables that a notification has not been seen by the session user? Clearly, posts seen and unseen will differ among users, and I cannot seem to wrap my head around this. I have tried the following:
Time stamps comparing the user's last activity and the notification activity. The notification will go away immediately after the user simply goes to any other page, not the location of the notification.
Location of the notification versus latest page user was; Every time the user visited the page with recent activity, the notification would go away, but trivially, it would appear again after the person left
So how do I simulate the 'read/unread' status of users and their notifications? Thanks everyone.

With your setup I would create another table users_notifications or something, with the columns id, user_id, notification_id, status, timestamp where user_id and notification_id are the unique id's to their tables respectively and status is an id or enum that identifies the current status of the user with that specific notification. This would enable you to easily calculate what you need now, but also grow into more if needed.

Related

How to handle users joining a game room

I know this question is not coding specific and might be broad, but I just want to know the thinking process because I am a bit lost.
I am trying to make a simple board game with Ajax and PHP. I know making games with web sockets would be far better for real time experiences but for now I am keeping to Ajax and PHP only. Till now I have been able to create rooms for users but cant understand how to make other users join that room and account for their details like name, score etc.
My situation:
I have an index page where users visit, and then the user fills in his name and chooses board size, then he is redirected to another page with a link like playground.php?room=123456
Another user uses that link and enters that room but then I don't know how to identify that second player and store his details like his name etc to the db.
I should store data in a database and cookies.
In the database, you have 3 tables
tblUsers (username, userid, ip,...)
tblRooms (roomid, creatorid,..)
tblPlayers (roomid,userid,..)
If a user visit your webpage, check for a cookie that has the userinfo.
If there is no cookie let the user login or register.
If a user creates a room, store this data in tblRooms.
Now he can share the link.
Another player visits the room. First you have to check if the player is registered (check for cookie). If not, let him show a registrationform.
If registered, store the user id in tblPlayers. With that table you know the players for that room.
I am not an expert in PHP of course, but I would do this:
1) When a user fills in details in index page, you can save some basic info in local storage or using cookies, so you can get those details later.
If you have a database, then you can have a table in which you will save users...
2)When that user enters playground.php?room=123456 you can get those details you saved in localstorage or cookies and add that user to the database.
3) You check the database for the player who created the room every x amount of time and if there is a user you encounter for the first time, then you can do some action..
I hope this helps you a bit :)
Good luck with you project ;)

Need help setting up a logic behind a Facebook notification system style

I want build a notification system to my website, similar to Facebook. The notifications don't need to be in real time.
This is what I have in mind:
User create an event (upload a new photo, add a new comment, like a photo, or even an administration alert to all users)
Run a cronjob every 5 minutes to add the notifications into the notifications table: id|id_content|type_of_content_enum|affected_user_id|date|seen_bool
The cronjob, will run several functions for each type of notification, for example:
add_photos_notification() // This function will check all photos added in the past 5 minutes, and insert a row in the notification table, for each user following this person. The function will group all photos added into the past 5 minutes, so the follower don't get many notifications for the same type of content. Resulting in a notification like: User X added Y photos in his profile.
add_admin_notification() // This function will check all news added by the administration of the site in the past 5 minutes, and insert a row in the notification table, for each user on the system ...
Is this a correct approach to build a notification system?
Is it possible to miss an event, running a cron every 5 minutes where functions retrieve the past 5 minutes events?
To be safe, do you think an alternative could be checking all events where a field 'notified' is not true? The same function that will grab the events where 'notified' = false, update it to true after adding that notification into the notifications table.
Thanks,
I went with the cronjob route and working good so far. Since our system got so many users to be notified, I found it the most appropriate way to do for two reasons.
I don't need to edit my current scripts code, inserting functions to add notifications for every event I want notify.
Since there gonna be some actions where many users are affected, adding notifications in real time could result in long script delays and time outs.
I built a class called notifications and inside this class, there are functions to add notification for every event I want notify, for example: user_added_a_new_photo(); user_commented_on_a_photo();
For every notification generate, Im adding 1 entry per user to be notified. This is how my notifications db looks like:
id
affected_user_id //user being notified
user_generating_the_notification_id
content_type // enum containing all type of notifications my system has (example: photo, video, comment) ...
content_json // a json containing the notification content. Based on the content type, on the display view file, I call helpers that will format the notification row using the json info.
date // the date the notification was added
seen_on // the date the user saw the notification
clicked_on // if user clicked on the notification, store the date he clicked on it
display // true or false
For this purpose, I added the display field cause for every new notification I create, I check the database if the same user, has another not seen notification, from the same generating user. If this condition is true, I set the old notification to display = false, and group the two new notifications resulting in something like: User X added X new photos in his gallery.
The clicked_on field, stores the date the item was clicked so I can generate reports based on this info if I need to. When displaying the content, if this item is not null, I highlight the notification to mark those not checked yet.
I created a text field to store the notification content in json, cause different notifications has different styles to present the users. For example, a new comment notification, has only texts, but a new photo notification, has a preview thumb.
So far, no issue running it and working for my needs.
The only downside, since the cronjobs can only be run only every 1 minute, the notifications may have 1 minute delay. But since I don't need it in real time, I set the cronjob to run every 5 minutes.
I've been looking into something like this as well and i just found this question on stackoverflow
Building a notification system
Have a look at the answer, the user does shed a fair bit of light on the theory of how to implement this sort of system.
As far as i can see you would need to create a record or object for each notification, if 100 people are subscribe to that event then 100 records will be generated. When a page is loaded your system will find all the notifications that correspond to the user logged in (Maybe by record ID) and then you would notify the user of how many notifications they have.
Now another way of doing this (Note i haven't implemented any of these, they are just ideas) would be to make a notification in a a table, then we'd have a second table that will hold a users ID and a notification ID, a user will be added to this table when they are subscribed to a notification, for example, confirmation of a friend request.
Please note again, these aren't proven methods, they're a result of some of my research of the matter, i'd read the post i gave you before doing anything.

Approving information prior to storing in database?

I am developing a web-based iPhone app and possibly a PC friendly website version as well. The goal here to to allow users to submit a form where specific input values would be stored into a table in the database.
Mind you this information is being gathered for public display and will be posted onto a calendar or list.
However, to prevent from any trolling or spamming, I'd like to make it where submissions have to be approved prior to being submitted into the table.
I have no problem with creating the table, connecting to the database, storing input values into the corresponding table columns. The only issue is how would I go about setting up an approval system? Can I add information to a table via email? Is there a way to approve admissions in cPanel?
This is something that I would like make as smooth as possible, I am expecting a lot of submissions daily with quite a bit of information.
You can have two approaches for this.
Approach 1
Have two copies of the table (which you want to save information
into). The first one should be named tableName_Input. The second one
should be tableName_Final.
Any Data in '_input' is considered raw and needs approval. Once approved the data will be moved into '_final'. The LIVE list/calendar always read from '_final' data.
Approach 2
Have a column named 'isApproved' with a flag 0/1. If 1 it is approved, else it is not. Only show data that is Approved.
Now, how do you get the data approved ?
You have a hard fast rule like spam filter that tells certain post is valid and approved by default
After every post, you send the user an email or some notification (unique to the user - post) that when answered back, shall mark it as approved.
Optional: You can place a column called as 'approval comments' to fill in something at the time of approval.
Flow chart
Tables
'FirstSubmitContent' - Table to store user submitted information
prior to approval.
'FinalSubmitContent' - Table that stores the final information
Code Pages
Content Page --> Contains the form the user fills the content
ContentActionPage --> Calls the controller --> calls the Model
Controller --> calls the model based on page action
Model --> Interacts with the Database table
I do not have any tools at my disposable now to write more detailed Code or Flowchart. I hope this puts in the right direction.
Validate the form on submission and save info in a temporary table in your DB with a randomly assigned activation code (you could use sha1). Then send an email to user with activation code and a link to verify it, ie. domain.com/activate.php?code=abcde12345.
The activation page can be very simple with just a $_GET['code']. Then check if you find a match in the DB for that code and finally prepare your query with all the info you gathered before to store it permanently.
Then you can make a cron job to delete all records from that table every 24-48 hours so users will have to activate within that time range.

New posts notification with PHP and MySQL

I know how to implement things in PHP and MySQL, but now i have a little thinking problem.
There's a forum-website. Every user could post a question (like stackoverflow). So how could it be done that when the user returns (after 2 days, or something) that he sees (a message, or alert...) that he has new posts on his thread?
Do you know what i mean? I can't put that all into a database, I think that's a little bit server-heavy??
Thanks.
If I recall correctly, here's how Simple Machines Forum handles it.
Each user has a record for each thread that they've clicked on. The record stores the highest message ID for that thread at the time it was clicked.
Since message IDs are auto incrementing integers, any new message will have a message ID larger than the previous.
So, when the user views the list of threads, SMF sees if the current highest message ID in the thread is larger than the one stored in the user's thread record. If it's larger, there are new records, and the thread gets a "new messages" icon. Otherwise, the thread gets the "no new messages icon". Also, if there is no thread record, then the user never visited the thread, and it gets a "new messages" icon.
When the user actually clicks on the thread, the thread record is updated with the highest message ID again.
You should store the lastActiveTime of the user on the site. Every time the user visits a thread or a page, update this value for them. The next time they return, do a query for all posts created after the lastActiveTime and present a list of links.
This means you only need to add one field to the user table, and nothing more. This new field contains a simple date from which you can determine the freshness of certain posts.
You can put that all into a database...
Where else would it all go?
Magic Land? ^_^
Just store it all in the database with some sort of time element added to each row.

Recent profile visitor and mailing system

Hi there I am creating a community website where if user A visit to profile of user B then it will notify user B that 1 person visited to your profile, if user is offline then it will send email notification that 1 person visit to your profile.
Problem: If I enter a log into database every time user visit to anothers profile... it will show wrong data.. for example if i visited to a user profile and just refresh page many times then it will log many times and i have to use grop by query which is waste of data. So should I use time to something? what will be best approach?
Second, how frequent I send email if user if offline. If i send once i enter log then in a day I am sending many mails to users. What should i follow to keep system spam free and effective?
First:
The data is not wrong! YOU must decide what you want to log! If you want to log only one visit from A to B per Day add a date column to your log and check if thee is already a row With matching "from", "to" and "date" values before you insert a new row.
This would however create two rows if A visits B at 23:58 and reloads at 00:01.
You can add a datetime column to your log and only insert a row if the timespan between two visits is greater then or equal to 24 hours.
Second:
Let your users choose how often they want to get a notification! You can use cronjobs and/or workers to send the notifications depending on the users settings. I would suggest using one worker to do all the mailing to avoid conflicts, but this highly depends on your server. If you can not set up a dedicated mailing thread you can use multiple cronjobs but then you will have to take into account that a user might change his settings, or get a visitor while your cronjob is still working.
IMHO Sending notification mails more than once a hour for profile visits is a reasons to block all mails from a site. Daily updates should be enough.

Categories