So I'm wondering how sites like YouTube remember when a user has "Liked" a video and prevents them from liking it again. Similarly, how a site like Reddit remembers upvotes and downvotes and prevents a user from upvoting a piece of content they already upvoted.
Is it as simple as a database table where the ID of the content is stored along with the ID of the user and their response? I feel like that would become a massively huge table. Is there something trickier going on?
The sites require you to login before clicking on "like" and "upvote". The content document in the DB will have a field which will store the number of likes received. Before the like button is rendered, the sites will check against the logged in user's records in the DB, to check if he has already liked it- accordingly a "like" or "unlike" option is displayed.
If you check here in Stackoverflow, if you click "upvote" on your own question or answer, a message is displayed, that you can't upvote our own post. What happens is when you click "upvote", ajax request will be sent to server with the user ID and other information like question id etc. and the server code will check if you are allowed to upvote i.e your user ID should not be the same as the post creator's ID.
I have a like/dislike system on my page.
Database tables:
1.) One that contains your posts with a unique ID for each post and a user_id of who created it (along with other info like content, tags, etc).
2.) table called likes with AT LEAST the following fields, ID, post_id (corresponds to the post in the post table that was liked or disliked), user_id (corresponds to the user in the users table that did the liking/disliking), status (0 or 1, 0 being liked the post, 1 being disliked the post).
When a user likes a post, insert the row into the likes table with their user_id and the post_id, set the status as 0 (or just leave empty because 0 is the default). When a user dislikes a post, do the same but set the status as 1.
That way, on a post page you can get the count of all the users that liked or disliked a post. On a users's profile page, you can get all of the posts a user either likes or dislikes. You can also rank posts by which has the most likes or dislikes. Or even rank specific users by who has posted content with the most likes or dislikes.
Do not allow a users to like/dislike a post if they already have a record in the database. (Basically just check the count of records in the likes table where the post_id is equal to the current post and user_id is equal to the logged in user)
Cross reference the post table to get the post's author's user_id. If the post author user_id is the same as the logged in user, or the user is NOT currently logged in, do not allow them to vote.
The queries for doing all of those are simple (simply SELECT * or SELECT user_id) but that is the basic idea.
Yes, it's that simple. Generally, sites use ip addresses though, so that users don't vote twice on different accounts.
edit: Sorry, I was wrong. According to Quentin, websites don't base it off IP because of the possibility that multiple users have the same IP and aren't trying to exploit the system. Smaller scale sites (at least some of the ones I've used) have implemented a voting system based on IP because it would otherwise be easier to manipulate content ranking.
Related
So I have two different tables, a users table and an articles table. The idea is to allow a user to rate an article, but only allow them to rate it once (possible change their existing rating too but I can come to that conclusion later).
As of now I just have the update value working to allow them to rate the article, but of course a user can rate an article as many times as they want.
To give you an idea of how I have everything working, when a user logins in, a session is created with their user information. So when they go to rate an article, I have the ability to check the user, I just don't know how to stop them from rating if they have already rated a specific article.
The user table consists of among other things their username and their unique ID
and the article table consists among other things the article contents, the article unique ID, and the articles rating.
I had some really sloppy ideas like when the user rates an article their ID gets stored into the articles row in some kind of "users who have rated" column, and then I can do a for loop or something to siphon out all the user IDs and then check if their ID exists in that articles entry but then each article would have a row with possibly hundreds or thousands of userIDs on it and there seems like there would be a more elegant way.
Any help or direction is appreciated :)
Create a UserRatings table which has foreign keys to the users table and the articles table, and stores a row linking the user to the article, and the rating they gave it and when it occurred.
Then if a user tried to rate it again you just check this table for the user ID/article ID combination before allowing it.
And then if you wanted got can do things like show the user a list of articles they have previously rated, etc
The site is a voting site for music artists. Each artist is saved in a database and they have a vote column. Each time a user clicks vote it adds one to the vote column in the database.
I want the users to log in with facebook and vote only ONCE for only ONE artist.
How would i go by doing such thing?
EDIT
This is a screen shot of the database I have.
database screenshot
Not sure where exactly you got stuck, and your question is way too broad for Stackoverflow, but here's how it works:
Authorize users with your Facebook App.
Whenever a user votes, store his ID with the vote and check if he already voted for that artist. A voting table could have the following 3 columns: artistId, vote, userId - or if the voting is not a number but just a flag, you could omit the "vote" column.
Links:
https://developers.facebook.com/docs/facebook-login
http://www.devils-heaven.com/facebook-javascript-sdk-login/
I am working on a social network website project. I have created database and everything.
The posts table has a preference column which stores the preference value according to the likes and comments that a post gets from the users and also the time at which the post is created.
To retrieve posts for a user's home page from the posts table, I am running a query using joins which sorts using preference column .
Now, suppose I retrieve 10 posts for a user to be shown on the posts table and user scrolls down and one more request is made from the user to retrieve next 10 posts to the server.
If in between of those requests few other users creates a new post or preference value of posts in the database changes in the between, and now if I the second request is run on the server, all the posts will be resorted for the second request (i.e. to show next 10 posts) but since the database is updated , this means in the second request there will be many chances that few of earlier 10 posts are retrieved along in the second request.
I want to know how to avoid these duplicate requests.
How facebook or any other social network solves this problem at the backend when their database is dynamic.
I would rather avoid such unreliable way of sorting at all.
As a user, I'd rather quit that service. Frankly, I hate such too smart a service which decides which posts I have to see and which not. And even dynamically ordered on top of that.
Make it ordered by date, by tags of interest, by something sensible, reliable and constant.
In your script store a record of the rows id returned.
For example, using a basic limit and just storing the latest id when the first select is done, and using the page number to determine the limit of records to return.
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT $PAGENUMBERTIMESTEN, 10
or storing the latest id after each page is returned (which you will need to store each time this is run)
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT 0, 10
If you store the time & date when the user first makes a request in a session, you could use that to filter the posts table.
So your SQL for the second page of results would be along the lines of
SELECT <some fields> FROM <sometables>
WHERE DatePosted <= $timefirstseen LIMIT 10, 10
Where $timefirstseen was loaded from the session variable. This will restrict your results to only posts that existed when the users visit started.
You would of course need to include a feature to allow the user to clear the session or do that automatically when they revisit their homepage to make sure they got to see the new posts eventually!
As title, when I have a content list, but not everybody can see them.
For example:
I have group 1 and group 2
I create a post, only the users in group 1 can see it.
I create another post, only the users in group 2 can see it.
User 1 in group 1, User 2 in group 2.
Then, when user 1 looks at the newest content list, he can only see the post 1.
Then, user user 2 looks at the newest content list, he can only see the post 2.
You can see, different account (user) will see different post in his/her post list. Because I store all posts in one table, and I store the group_id in it, when creating the newest post list for a user, I have to search all newest post and check one by one if the user can see it or not, if there are too many posts which he don't have permission, the performance will be very bad.
Also, the group is not static in some cases, such as: my followers, my friends, if so, I still can't store the user id in post table, and have to calculate their relationship to determine he/she can read this post or not. and again, the post can be with multiple-permission groups (my followers and my friends can see it).
How can I improve it?
BTW, why I don't insert the user_id array in post record, because maybe the number of group users is very big, so I can only save group_id in it.
BTW again,
I have one SNS website.
I use mongodb database.
I use php.
When user authorizes, store his group_id in session data. Then you can use that group_id when you generate your menu, make it an extra condition in your database query to get only posts that are allowed to this group to be seen.
If the user is not authorizes, you can use some default group_id value to show only posts, allowed for everyone to view.
For complex conditions like the ones you have I'd recommend using UNION if you had mysql, but looks like you will simply need to do multiple queries. First, separate all possible different conditions on which content groups should be available to a specific user.
For example: if user can only see content from his group, his friends and people he follows, you can simply do 3 requests (get content list of his group, posts of people he follows and posts of his friends) and then combine that data with PHP and output it. Since all the data is the same (as you said - it all is in posts table) you won't need anything fancy here. Just three request and sorting the way you want on php side.
Is there a way that will allow me to limit the amount of profiles a user can look at on my site per day. so for instance each user has an id 1, 2, 3 etc. and if the one user views 5 profiles all together in one day then it stops them viewing any more and redirects them to a sign up page to become a paid member where they can view unlimited profiles?
I'm quite new to php and sql but this is primarily what i am working in if there's a way to do it in that.
Thanks
At your database create 1 table called user_views with 3 fields
id (auto increament), user_id (the user who is visiting), visited_user_id (the user id who is visited)..
At your user_details page which users see other user, at the start of your code set 1 function which will add that view to this table , if user has already visited this user it must ignore it and if user has make all allowed visits this function will redirect him ...
And the db table must be truncate each day at 00:00:00..
I think a simple use of SESSION variables and a counting mechanism would suffice.
yes you can limit them in viewing,
you can save it in database and every time they view each profile then save it into ur database, you can make a table,
tbl_user_view
field: user_id, view_count
every profile view make an update function wich
UPDATE tble_user_view set view_count = view_count + 1 WHERE user_id = user_id
and here's some twist, if the user viewed the same profile twice you need to record it as 1,
so save the id of the profile that they viewed in COOKIES or if you do not care on database load you can save it there.
Yes. Each time the user views a profile, store that information (viewer, who they viewed, a time stamp).
Every time they go to view a profile, check how many profiles they have viewed today. If it's over the limit, redirect them.
There's other logic you may want to consider, for example, if they come back to the same profile 5 times, does it count each time?