到目前爲止,我認爲我做得很對,但我無法正確顯示grade_points
這裏是我在下面顯示的內容。我如何解決這個問題,以便我的代碼能夠正確顯示grade_points
?
如何使用PHP和MySQL正確顯示學生的總分分數?
輸出數據
這裏是我的電流輸出:
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
,這裏是我想要的輸出:
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代碼
這是我的PHP & MySQL的代碼。
$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表
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
等級
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
哇,這是一個很大閱讀。而我不知道你的實際問題是什麼>< – Jeriko 2010-06-22 19:07:47
我在想如何修復我的代碼,以便我可以正確顯示'grade_points'? – codeMONKEY 2010-06-22 19:14:22
你應該如何做到這一點,而不知道作業的權重是多少?或者他們都值得一樣的金額?而且什麼是grade_average? – 2010-06-22 22:55:36