I don't even know what questions I should ask. Well, I want to create a thumbs up for my comments, but not sure how or what's the best way. Do I just create a new field for thumbs up?
It depends on what you want to do with this. Why not just put an int
column on your comments table, storing the total number of thumbs up / down for the comment?
Just storing an int for the number of times a comment has been voted up would be subject to abuse. You probably also want to associate each vote with the user who cast it, that way you can prevent people from repeatedly voting for the same comment.
For this to work, I think you'll need a separate table for votes. Each record in that table should have the comment id and the user id of the person who cast the vote.
If you need to keep track of who's voted on what, you should perhaps make a Votes
table:
vote_id
: Primary key.user_id
: The id of the user who made this vote. [Foreign key toUsers
table.]comment_id
: The id of the comment that was voted on. [Foreign key toComments
table.]vote
: The vote that was cast (perhaps +1 or -1 if you only have a trivial thumbs up/down system).date
: When the vote was cast.
A comment's score is now just the sum of all the vote
columns which have that comment_id
.
Note that unlike simply adding an integer score
column to your Comments
table, this has the advantage of telling you the level of controversy a comment is experiencing. Without knowing how many votes were cast, two comments with a net score of zero could either be experiencing a lot of controversy (people are equally split about the merit of the comment, so the total score hovers around 0), or none at all (nobody cares enough to cast a vote).
Creating a separate table, as Bill and John have suggested, would probably be the best approach. But you might still want to add a votes
column to the comments
table for performance reasons. This way, you won't need to access the votes
table when you only want to display the vote count for a comment. I believe this is how votes work on SO.
Create 2 fields in your comment table, vote_up
and vote_down
, and increase their counters accordingly upon user's vote, this way you can display comment score as sum of these values or as a percentage, in the later case you could add third field vote_score
which stores percentage score if you ever wanted to be able to sort by score.
Then create votes table to prevent users voting twice the same comment, ever or in given time span, if so just set cron job to run once a day and delete entries older than time()-( 86400 * DAYS_TO_KEEP_VOTE )
- comment_id
- user_id
- vote_time
Good luck.