简体   繁体   中英

How to display a students total grade points correctly using PHP & MySQL?

So far I think I'm doing it right but I cant seem to display the grade_points correctly here is what I'm display below. How can I fix it so that my code will display the grade_points correctly?

Here is my current output:

user_id Array ( [1] => 1 [3] => 2 )
rank Array ( [0] => 1 [1] => 3 )
grade_points Array ( [0] => [1] => )
users Array ( [0] => 3 [1] => 2 ) 
the rank of user_id 3 is #2

And here is what I want to output:

user_id Array ( [1] => 1 [3] => 2 )
rank Array ( [0] => 1 [1] => 3 )
grade_points Array ( [0] => 8 [1] => 5 )
users Array ( [0] => 3 [1] => 2 ) 
the rank of user_id 3 is #2



Here is my PHP & MySQL code.

$i = 1;

$u = array();
$user = array();
$rank = array();
$gp = array();

$dbc = mysqli_query($mysqli,"SELECT SUM(grade_points) as p, grades.grade_points, assignment_grades.*, COUNT(*) as u, users_assignment.user_id
                              FROM users_assignment 
                              LEFT JOIN grades ON users_assignment.user_id = grades.letter_grade
                              LEFT JOIN assignment_grades ON grades.id = assignment_grades.grade_id
                              GROUP BY users_assignment.user_id
                              ORDER BY p DESC");


if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($dbc)){ 
        $u[$row['user_id']] = $i++;
        $rank[] = $row['user_id'];
        $user[] = $row['u'];
        $gp[] = $row['p'];
    }
}

echo 'user_id '; print_r($u); echo '<br />';
echo 'rank '; print_r($rank); echo '<br />';
echo 'points '; print_r($gp); echo '<br />';
echo 'users '; print_r($user);

echo "the rank of user_id 3 is" . $u[3];



CREATE TABLE assignment_grades ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
grade_id INT UNSIGNED NOT NULL, 
users_assignment_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_assignment (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
assignment_content LONGTEXT NOT NULL,
grade_average VARCHAR DEFAULT NULL,
PRIMARY KEY (id)
);



assignment_grades

id      grade_id        users_assignment_id
15      15              35
16      16              35
17      17              33

grades

id      letter_grade        grade_points
15      C+                  3
16      A+                  5
17      A+                  5

users_assignment

id      user_id     assignment_content      grade_average
32      1           some content            NULL
33      1           some content            A+
34      3           some content            NULL
35      3           some content            B+
36      1           some content            NULL

I think the problem is in your query. Try to replace SUM(grade_points) to SUM(grade.grade_points)

Your query's joins are not matching the fields properly -- try this for your query:

SELECT SUM(grade_points) as p, grades.grade_points, assignment_grades.*, COUNT(*) as u, users_assignment.user_id 
FROM users_assignment 
LEFT JOIN assignment_grades ON users_assignment.id = assignment_grades.users_assignment_id
LEFT JOIN grades ON grades.id = assignment_grades.grade_id 
GROUP BY users_assignment.user_id 
ORDER BY p DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM