我正在制作成绩册,虽然下面的代码有效,但速度非常慢。表格的第一列是每个学生的姓名,然后每个单元格后面对应一个特定的问题,并显示学生的答案是什么。不是每个学生都会回答给定的问题。
现在我有一个循环,对于每个用户,每个问题在该循环内都有一个循环。
每个问题都有一个唯一的 ID,在单独的表中每个答案都有一个相应的用户 ID 和一个相应的问题 ID。
我一直在寻找类似的帖子,但没有运气,我只使用 php 几个月,所以任何建议都将不胜感激。
问题
| 编号 | 问题 | 选择1 | 选择2 | 选择3 | 选择4 | 正确答案 | 类别 ID |
答案
| 编号 | 问题ID | 用户名 | 学生回答 |
用户
| 编号 | 名字 |
谢谢
//Select all questions from a specific category $mid defined by user
$questionQuery = mysql_query("SELECT * FROM questions WHERE mid = $mid");
$questionCount = mysql_num_rows($questionQuery);
//Blank space in table above users
echo "<tr><td></td>";
//Question Number, each category has a different number of questions
for ($q = 0; $q < $questionCount; $q++){
echo "<td>" . ($q+1) . "</td>";
}
echo "</tr>";
//Select all students for a given teacher
$userQuery = mysql_query("SELECT * FROM users WHERE teacherId = $userid ORDER BY `block`, `lastName`");
$userCount = mysql_num_rows($userQuery);
for($s = 0; $s < $userCount; $s++){
$currentUserId = mysql_result($userQuery, $s, 0);
//Echo student's name
echo "<tr class='user" . ($s%2) ."'><td>" . mysql_result($userQuery, $s, 1) . "</td>";
//Load current question id
for ($q = 0; $q < $questionCount; $q++){
$questionNum = mysql_result($questionQuery, $q, 0);
//Selects students answer from answers database for above question
$answerQuery = mysql_query("SElECT * FROM answers WHERE questionId = $questionNum AND userId = $currentUserId");
$answerCount = mysql_num_rows($answerQuery);
//If no answers move on to next cell
if($answerCount == 0){
echo "<td></td>";
}
else{
$currentAnswer = mysql_result($answerQuery, 0, 3);
$correctAnswer = mysql_result($questionQuery, $q, 6);
if ($currentAnswer == $correctAnswer){
echo "<td class='right'><div id='parent'>" . $currentAnswer . "</div></td>";
}
else{
echo "<td class='wrong'><div id='parent'>" . $currentAnswer . "</div></td>";
}
}
}
echo "</tr>";
}