tags:

views:

111

answers:

2

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 '<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];



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
A: 

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

GOsha
No success with `SUM(grades.grade_points)` :(
codeMONKEY
Why is so strange structure of tables? Why not to add usr_id field in grades table, and you can work without assignment_grades. The query here becomes much easier
GOsha
A: 

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
Dan U.
This code seems to cause havoc with the rest of my code but it does get the grade_points correctly. Is there a way that i can change it so it wont cause problems with the rest of the code but still display the grade_point correctly?
codeMONKEY
Hmm, I'm confused by your expected output -- since user 3 had two assignments totaling 8 points, and user 1 had one assignment totaling 5 points, it would seem that the rank of user 3 should be #1 instead of #2, since you're ordering by total points descending? Or am I missing something?
Dan U.
here is a brief explanation 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.
codeMONKEY
@DAN U but nothing else is displayed correctly only the grade points when I use your code.
codeMONKEY
I think you'll have to reconsider your database design to accomplish what you're after. Your original query was doing strange things like trying to join on disparate fields (users_assignment.user_id = grades.letter_grade) so any results you initially got were just happenstance.Are letter grades always assigned the same number of points (A+ = 5, A=4.5 etc.)? If so start by having a gradepoints table with just those two fields. Then assignments can have a grade column that joins to the letter grade, to pull in the points from that table, etc.
Dan U.
assignments have a grade average column as for letter grades there is already a table called grades.
codeMONKEY