Jump to content

Determine rank within series of numbers


bartibus

Recommended Posts

Hi,

Does anyone know how I can make a rank list from a series of numbers? I have a competition in which participants can gather points; in a mysql database table there's a row for each participant (users) and a column in which the point totals are saved (totals). When I use 'ORDER BY' to sort the users according to their totals, the rank is consecutive, even when two or more users have the same amount of points. Is it possible to make a script that sorts the users according to their totals and assigns a rank number, that remains the same if two or more users have the same total (and skips the relevant rank numbers)? Below is an example of what I would like to have:

1 User A 200
2 User B 150
3 User C 125
3 User D 125
3 User E 125
6 User F 100

I hope that someone can help me with this,
Thanks in advance,
Bart
Link to comment
Share on other sites

try

[code]<?php
$scores = array (
    'User A' => 200,
    'User B' => 150,
    'User C' => 125,
    'User D' => 125,
    'User E' => 125,
    'User F' => 100
);

$rank = 1;
$prev = 0;
$count = 0;
foreach ($scores as $name => $score) {
         if ($score==$prev) {
             ++$count;
         }
         else {
              $rank = ++$count;
         }
         echo "$rank $name $score<br/>";
         $prev = $score;
}
?>[/code]
Link to comment
Share on other sites

Thanks,
The problem is though that the scores are variable; the user A, etc. was just an example. I have tried to change the code so that it will retrieve the users and scores from the database, but the resulting list gives every user the same rank. This is my code:
<?
$query = "SELECT username, name, totaal FROM punten ORDER BY totaal DESC, name ASC";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc ($result)) {
$username= $row['username'];
$name= $row['name'];
$totaal= $row['totaal'];

$scores = array($name => $totaal);
$rank = 1;
$prev = 0;
$count = 0;
foreach ($scores as $name => $totaal) {
if ($totaal==$prev) {
++$count;
}
else {
$rank = ++$count;
}
echo "$rank $name $totaal<br />";
$prev = $totaal;
}
}
?>

Do you know what is wrong with my code?
Link to comment
Share on other sites

If you are doing it straight from the db table, sorted, you can drop the array code.

[code]$rank = 1;
$prev = 0;
$count = 0;
while ($row = mysql_fetch_assoc ($result)) {
    $username= $row['username'];
    $name= $row['name'];
    $totaal= $row['totaal'];

    if ($totaal==$prev) {
        ++$count;
    }
    else {
        $rank = ++$count;
    }
    echo "$rank $name $totaal<br />";
    $prev = $totaal;

}[/code]
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.