Click here to Skip to main content
15,033,219 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Greetings, Please i need a help on how to rank students based on their total scores, i have table where their scores are stored, therefore, i want to be able to determine the rank/position of the individual student based on his/her total score.

My Table:

student_regno| academic_session| academic_term| class_name| class_group| total_score
OUT20267 2016/2017 First-Term Primary 6 A 480
OUT20336 2016/2017 First-Term Primary 6 B 520
OUT20139 2016/2017 First-Term Primary 6 A 593

I want something like this when i fetch it:

OUT20267 = 3rd; OUT20336 = 2nd; OUT20139 = 1st and so on...

What I have tried:

$class_name = $conn->real_escape_string($_POST['h_class_name']);
$class_group = $conn->real_escape_string($_POST['h_class_group']);
$student_regno = $conn->real_escape_string($_POST['h_student_regno']);
$academic_session = $conn->real_escape_string($_POST['h_academic_session']);
$academic_term = $conn->real_escape_string($_POST['h_academic_term']);

$data = "SELECT total_score FROM student_compile_result WHERE student_regno ='$student_regno' AND class_name = '$class_name' AND class_group='$class_group' AND academic_session = '$academic_session' AND academic_term = '$academic_term' ORDER BY abs(total_score)";
$datas = mysqli_query($conn,$data);
$row = $datas->fetch_assoc();

//while(list($student_regno, $class_name, $class_group, $academic_term, $academic_session, $total_score) = mysqli_fetch_row($datas))


echo $student_rank;
Updated 18-Apr-16 4:02am

1 solution

Are you hoping to obtain a sum without using SUM? :-)
MySQL SUM Function[^].

Chidiebere Ezeka 18-Apr-16 10:16am
I have the total scores for all students, but i need to rank them based on the highest score, such as one with the highest score will be ranked as 1st.. and so on.
Sergey Alexandrovich Kryukov 18-Apr-16 10:31am
I don't see where you have those totals. And what's the rank? If this is the integer value depending on the scores of all the student, next step would be supporting re-ranking each time you change the score of any single student, but this is a non-query statement...

If you don't need to store ranks in the database, and only rank the data after query on the fly, it would be much simpler. You can do it on the PHP side, to the data your receive according to ORDER BY...


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900