Du skal bruge to tællere
- Absolut tæller (altid 1, 2, 3, 4, 5 osv.).
- Rangtæller - den tæller fra 1, men hvis scoren er ens, opdateres den ikke. Så snart resultaterne er forskellige, opdateres den med den
absolute counter
.
Eksempelkode
$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