Programmer Question
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?
OUTPUT DATA
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
PHP & MySQL CODE
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 '
';
echo 'rank '; print_r($rank); echo '
';
echo 'points '; print_r($gp); echo '
';
echo 'users '; print_r($user);
echo "the rank of user_id 3 is" . $u[3];
MySQL TABLES
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)
);
TABLE DATA
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
Find the answer here
No comments:
Post a Comment