Simple Voting - Storing in Database

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Simple Voting - Storing in Database

Post by jraede »

I'm working on implementing a voting system, similar to FMyLife or Texts From Last Night or those kinds of sites, with just a simple "thumbs up" or "thumbs down." I want to limit users to voting for something just one time, and I'm trying to figure out the best database structure to accomplish this.

I have a table of "places" and another table of "users." I'm thinking either I add another table of "votes", with a column for user and column for place, and with each vote I can check if there's already an entry for that user + that place in the votes table. Alternatively, I could add a column for "yes votes" and one for "no votes" on either the users or places table, and store the database ids of the places or users, respectively, that received the vote or performed the vote, in an imploded array.

For example, if users 3 and 7 voted yes for place 120, the "yes votes" column in the places table for place 120 would read "3,7". Or, the "yes votes" column in the users table for user 3 would read "120", as would the column for user 7.

Any other ideas?

Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Simple Voting - Storing in Database

Post by Christopher »

I think a separate votes table with the place ID and the vote would make more sense.
(#10850)
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Simple Voting - Storing in Database

Post by davex »

Hi,

I totally agree with arborint the seperate table with a row for each vote cast (user + place + vote) is the best way to go.

Simplest to check if a user has voted, add a vote and tally your votes.

Cheers,

Dave.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Simple Voting - Storing in Database

Post by jraede »

Cool, thanks. I went with that, working like a charm.
Post Reply