Anda membutuhkan dua penghitung
- Penghitung Absolut (selalu 1, 2, 3, 4, 5, dst).
- Penghitung Peringkat - dihitung dari 1 tetapi jika skornya sama, tidak diperbarui. Segera setelah skor berbeda, itu diperbarui dengan
absolute counter
.
Kode Contoh
$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;
}
Keluaran:
Rank: 1, Score: 97.8
Rank: 2, Score: 96.1
Rank: 2, Score: 96.1
Rank: 4, Score: 90.7