Jump to content

Ranking/Voting System question


bpops

Recommended Posts

Hello,

I'm working on voting system for a site of mine. I already have a member database in place where people can post comments and such. But I want them to be able to rank certain items (a la amazon, five stars or whatever). But I was wondering the most space-efficient and time-efficient way of doing this was? I want members ONLY to be able to vote. But not more than once on any particular item. So do I just make a new mysql table with fields like:
user_name, user_id, item_id, rank

and then every time I load that item, i have to query for the ranks with that id, and then average them?
Or is there a better way to do this? Any ideas or suggestions would be helpful. thanks!
Link to comment
Share on other sites

Assuming that each row of your users table has an id field and that each row of your items table has an id field, I'd create a table:

[b]ratings[/b]
id int auto_increment non null primary key
UserID int non null
ItemID int non null
Rating tinyint
UNIQUE(UserID, ItemID)

I'm not sure that tinyint would be appropriate, but if you're storing values 1 - 5 you certainly don't need a regular size int.

The UNIQUE constraint is important IMO because it will prevent your application from inserting duplicate rows.  Thus if you try to insert for a user and the query fails, you'll know to try and update instead, assuming you want people to edit their previous ratings.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.