EchoFool Posted November 27, 2008 Share Posted November 27, 2008 I am trying to make a voting system where by a query lists who has the mosts votes from other users with a simple table like this: UserID | VotedFor And basically then i trying to query to get the UserID's from that table in DESC order. But i don't know how to collect each userid that has greater than > 0 and then also sort it in order of total votes nor how to count how many they recieved per userid.. The end result should show the example of: User 1 : Total Votes: 100 User 2 : Total Votes: 50 etc Whats the best way to do such a thing? Quote Link to comment https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/ Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 $query = "SELECT VotedFor, Count(UserID) FROM your_table GROUP BY VotedFor"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "User ". $row['COUNT(UserID)'] ." : Total Votes: ". $row['VotedFor'] ." "; } But i don't know how to collect each userid that has greater than > 0 A userid with > 0 votes? : $query = "SELECT VotedFor, Count(UserID) FROM your_table GROUP BY VotedFor"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ if($row['COUNT(UserID)'] > 0) { echo "User ". $row['VotedFor'] ." : Total Votes: ". $row['COUNT(UserID)'] ." "; } } Quote Link to comment https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/#findComment-699889 Share on other sites More sharing options...
EchoFool Posted November 27, 2008 Author Share Posted November 27, 2008 Thats not working on my screen its loading Count(UserID) as just counting how many users there are in the table regardless of who voted. The VotedFor field is the user ID that the person has voted for not how many votes they have tis why it might explain the confusion. =/ Quote Link to comment https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/#findComment-700438 Share on other sites More sharing options...
sqlnoob Posted November 28, 2008 Share Posted November 28, 2008 perhaps SUM and HAVING command are what you need, or perhaps you need to add a WHERE clause I'm not terribly sure what you want to achieve with only those 2 columns and what values they can have. Can we assume that VotedFor holds a UserID? Perhaps try GROUP BY UserID instead of VotedFor if that helps Here is a good tutorial for those commands. http://www.1keydata.com/sql/sql.html Quote Link to comment https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/#findComment-700929 Share on other sites More sharing options...
fenway Posted November 28, 2008 Share Posted November 28, 2008 I'm so very confused... how can your group by be getting any people with no votes? There wouldn't be any records in the table??? Quote Link to comment https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/#findComment-701001 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.