Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/134434-counting-who-has-the-most/
Share on other sites

$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)'] ."
";
   }
}

 

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. =/

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

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.