Monday, May 4, 2009

Php & mysql question: retrieve a specific student's rank among all other students through the exam score.?

There is a mysql table name called tests


and fields as follow;





table: tests


fields: tid, studentid, gid, lessonid, score





Each test is connected to gid (group id) group of exams by date..


studentid is connected to student username.





let's say 30 students took same group of test called test A


and every student has test score and everytime each student logs into their account they will be able to see their own ranking among 30 students.





In student's account it will show as


example:


My Rank: 30/5





30 stands for the total students took the same group of test which I retrieve it by studentid count() funtion where specific test group id is connected to each test. and 5 is the rank so that student is 5th among 30 students according to the highest score.








But how do I retrieve the 5? which is student's rank calculated with student's score?





Thank you for taking your time to read my question.


I hope I can get an answer

Php %26amp; mysql question: retrieve a specific student's rank among all other students through the exam score.?
I would:


First set a number var which will be used in your mysql results array - checking its reference..





$dbname = 'your_dbname';


$studentid = '3';





$loop_num = 1;


$result = MYSQL($dbname, "SELECT studentid, score FROM tests ORDER BY score DESC");


while ($row = mysql_fetch_array($result)) {


$id_student = $row["studentid"];


$score = $row["score"];


if ($studentid == $id_student){


echo $loop_num;


break;


}


$loop_num ++;


}


No comments:

Post a Comment