I have a website that picks an entry from a database at random and then displays it to the user. I would like to add a "top rated" page on which the best entries as chosen by an aggregate of user opinions are displayed. I want up votes and down votes as seen here on stackoverflow and on many other websites.
I am not very experienced with web development, so I'm at a loss about where to start. The idea I have is that clicking "up" will add 1 to the "votes" field in a table and clicking "down" will subtract one from the same field, and I'll tie it all together with an "id" field that references on which entry is being voted. Is there a more elegant solution? How do I get started?
edit: my server side scripting is in PHP and I'm familiar with jquery.
edit 2: Upon rereading, I realize how broad this question is. Sorry! Specifically, I want to know how to code the PHP and mySQL necessary to add and subtract votes to and from the "votes" table without refreshing the page.
You'll notice that SO remembers which articles you voted on, and how you voted, so there is a table that ties together the item being voted on, the person doing the voting, and what their vote was. An aggregate function in SQL can be used to tally the number of votes from this joining table.
Note that if the function you use to calculate your "top items" is expensive (i.e. if you have a lot of items, etc.) then you may need to modify your query and will probably want to cache the results for a reasonable time period before doing the calculation again.
didnt read it but it should pretty much cover it :)
http://ad1987.blogspot.com/2009/02/reddit-style-voting-with-php-mysql-and.html
alternativel you can go to google.com, type in "vote up down ajax php mysql" and press i'm feelink lucky.
to display your top voted questions you should consider using a bayesian algorithm so you don't end up with top questions that have only one vote or something.
didnt read it again but this might help (the sql query in the middle of the page looks good) http://www.andymoore.ca/2010/02/bayesian-ratings-your-salvation-for-user-generated-content/
精彩评论