[Solved] How to assign rank to students to where they share the highest rank when their scores equal?


You need two counters

  • Absolute Counter (always 1, 2, 3, 4, 5, etc).
  • Rank Counter – it counts from 1 but if the scores are same, it does not update. As soon as scores are different, it updates with the absolute counter.

Sample Code

$counter = 1; // init absolute counter
$rank = 1; // init rank counter

// initial "previous" score:
$prevScore = 0;
while ($go = mysql_fetch_array($avg))
{
    // get "current" score
    $score = $go['AVGFCT_10'];

    if ($prevScore != $score) // if previous & current scores differ
        $rank = $counter;
    // else //same // do nothing

    echo "Rank: {$rank}, Score: {$score}<br>";
    $counter ++; // always increment absolute counter

    //current score becomes previous score for next loop iteration
    $prevScore = $score;
}

Output:

Rank: 1, Score: 97.8
Rank: 2, Score: 96.1
Rank: 2, Score: 96.1
Rank: 4, Score: 90.7

0

solved How to assign rank to students to where they share the highest rank when their scores equal?