开发者

Developing a simple voting system in php need help

开发者 https://www.devze.com 2023-02-20 11:43 出处:网络
I need to set up a simple voting system for my application., My application consists of articles posted as well as comments. I would like to add voting abilities to both articles and comments and at t

I need to set up a simple voting system for my application., My application consists of articles posted as well as comments. I would like to add voting abilities to both articles and comments and at the same time be able to sort comments based upon highest voted etc.

I have the following restrictions i.e since the application needs users to log in - only logged in users can vote, secondly a user can vote on an item only once. Users can upvote or downvote or cancel a vote they've made.

What would be a decent table design for this, p开发者_运维百科lus I need the solution to be scaleable. Thanks for the advice


I think I would go with a join-table between the users and articles tables :

users_articles
  - article_id
  - user_id
  - score
  - date

With the following notes :

  • article_id is a foreign key to the article that gets up/down-voted
  • user_id is a foreign key to the user that voted
  • score is +1 or -1 depending on the vote
  • the primary key is on the two article_id, user_id columns.
  • a user voting on an article means inserting one line in this table ; canceling the vote means deleting that line (or setting a 0 score if you want to keep track of the fact the user has voted)


That's for votes on articles.
And I would do another users_comments table for the votes on comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消