I'm trying to get the rank of a certain student out of a class of a hundred, I want to be able to display the students rank.
I'm trying to get the rank of a student by grades and grade points. For example if student 1
has 2 A+ grades with a total of 10 points and student 2
has 3 B- grades with a total of 10 points student 1
will rank higher. I was wondering how would I be able to do this using PHP & MySQL?
Here is the PHP & MySQL code I have so far.
$gp = array();
$dbc = mysqli_query($mysqli,"SELECT grades.*, homework_grades.*, users_homework.*
FROM grades
LEFT JOIN homework_grades ON grades.id = homework_grades.grade_id
LEFT JOIN users_homework ON homework_grades.users_homework_id = users_homework.id
WHERE users_homework.user_id = '$user_id'
AND users_homework.grade_average = 'A+'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
$gp[] = $row['grade_points'];
}
}
echo array_sum($gp);
Here is my MySQL tables.
CREATE TABLE homework_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
grade_id INT UNSIGNED NOT NULL,
users_homework_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
letter_grade VARCHAR NOT NULL,
grade_points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
CREATE TABLE users_homework (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
homework_content LONGTEXT NOT NULL,
grade_average VARCHAR DEFAULT NULL,
PRIMARY KEY (id)
);