I want to ask, how to create a rank from a table of scores that I had from database? For example, the table of database has student_id, course_id, scores. I have create a php code to get these scores and accummulate it as Final Score. Then, I need to create who get the first rank, second, and more. Here's my sample code:
$q = mysql_query("SELECT * FROM tbstudent");
while($r = mysql_fetch_array($q))
{
$q2 = mysql_query("SELECT * FROM tbscores WHERE student_id = '".$r['student_id']."'");
$total = 0;
while($r2 = mysql_fetch_array($q2))
{
$total += $r2['scores'];
}
echo "<tr><td>$total</td><td>/* for rank*/</td></tr>";
}
So, I have get the total scores, but how to get the rank for these student? For example my result right now is (name, scores, rank):
1. Ryan - 235
2. Jack - 450
3. Dave - 140
4. Levi - 330
5. Kens - 350
And what I want to is like these:
1. Dave - 235 - 4
2. Jack - 450 - 1
3. Levi - 140 - 5
4. Kens - 330 - 3
5. Ryan - 350 - 2
Don't give me answer to get it from database using AVERAGE, SUM, etc, because I have another calculation for the scores. So anyone know how to create it with PHP?