Dethman Posted July 9, 2008 Share Posted July 9, 2008 I am trying to make a vote system for a game of mine I need to figure out how to check each user to see witch one has the most votes SELECT `username` FROM `user_users` WHERE `userid`='$USER_WITH_MOST_VOTES' How do I come up with the variable "$USER_WITH_MOST_VOTES" ? Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 9, 2008 Share Posted July 9, 2008 If I were you, I'd create an additional column on the user_users table, and name it vote_count. Everytime the user gets a vote, you will update the value in the database and increment it by 1. Then to check the user with the most votes <?php // db connection info $query = "SELECT MAX(`vote_count`) FROM `user_users`"; $result = mysql_query($query) or trigger_error(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted July 9, 2008 Share Posted July 9, 2008 I think you need to display the structure of the database. All votes should be placed at a different table, then check the number of occurences there from the list of users from the users table. Quote Link to comment Share on other sites More sharing options...
Dethman Posted July 9, 2008 Author Share Posted July 9, 2008 If at all possible how do i make that code grab the userid of the person with the max votes? Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 9, 2008 Share Posted July 9, 2008 SELECT `userid`, MAX(`vote_count`) FROM `user_users` I think you need to display the structure of the database. All votes should be placed at a different table, then check the number of occurences there from the list of users from the users table. This would makes things inefficient for a simple polling system. A seperate table would only be required if you wanted to record information about the user that voted, e.g. userid, date/time they voted, what they voted for etc.. or if you wanted to display 'pretty' percentage bar images etc.. for BOTH answers. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted July 10, 2008 Share Posted July 10, 2008 Well I have a voting poll running... and I store it that way for various reasons: 1. its more flexible to changes. 2. you can history of votes. 3. if i were to add a new voting poll for a different purpose then I don't have problem extending. 4. cancelling of votes (not deleting) for a certain poll is possible without disabling user account or cancelling other polls. 5. and etc. Usually, the poll i made, users can create new polls and questions and with that in mind, my database wil never be spoiled... unless there would be requests that would dramatically change the concept of the current existing poll. But if it would be the simplest form of poll, then Wolphie's suggestion would work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.