Jump to content

top 5 posters


didgydont

Recommended Posts

i have a site that i want to display the top 5 posters but i have no idea how or where to start

this is what i have to count a user post but i cant figure out how to display or sort 

 

$result3 = mysql_query("SELECT * FROM Xbox360 WHERE User='$user'")
$count = mysql_num_rows($result3);

 

i tried this but i dont think im even close

$query = "SELECT *, COUNT(Game) AS Game FROM Xbox360 GROUP BY User ORDER BY User DESC";
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result)){ 
//echo $row['User'].": ".$row['COUNT(name)']."<br />";
echo $row['User'].": ".$row['COUNT(Game)'] ;
} 

 

thank you for your time

Link to comment
Share on other sites

If count(Game) is the value you want to find the top 5 of, you can do:

 

$query = "SELECT *, COUNT(Game) AS Game FROM Xbox360 GROUP BY User ORDER BY COUNT(Game) DESC, User";

 

The ", User" means sort by user after sorting by count(Game).  That acts as a tie breaker in case people have the same count.

 

The value resulting from that query will be called "Game", ie:

 

echo $row['User'].": ".$row['Game'] ;

 

The reason for the is you said "count(Game) AS Game".  The "as" gives a name to the count value.

Link to comment
Share on other sites

Hmm, try this.  Unfortunately i don't have mysql to test with so I can't test myself.

 

$query = "SELECT User, COUNT(Game) AS Game FROM Xbox360 GROUP BY User ORDER BY COUNT(Game) DESC, User";

 

That's a vanilla group by that ought to work.. if it gives an error, try "ORDER BY Game" instead of "ORDER BY COUNT(Game)"

Link to comment
Share on other sites

thank you very much i will book mark this for next time i ended up making a function to do the lot though

function topposters($display){
$usersresult = mysql_query("SELECT * FROM Users");
$array = array();
//$result3 = mysql_query("SELECT * FROM Xbox360");
while($row = mysql_fetch_array($usersresult)){
  $user = $row['username'];
    $result3 = mysql_query("SELECT * FROM Xbox360 WHERE User='$user'");
    $count = mysql_num_rows($result3);
   //$array[] =  "Count => $count User => $user";
    $array["$user"] = $count ;

} 
arsort($array);
//$topposters = print_r($array);
//return "$topposters";
$i = 0;
Echo "<br><br><br><SPAN class='kinglahead'>Top $display Posters</SPAN><br>";
foreach($array as $key => $value) {$i++;
  if ($i<=$display){echo "$key(<a href='index.php?poster=$key'>$value</a>) ";}
    }
}

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.