以下に、Session、Question、Answer、および Option_Type の 4 つのデータベース テーブルがあります。
セッション テーブル:
SessionId SessionName
1 AAA
質問表:
SessionId QuestionId QuestionContent OptionId
1 1 What is 1+1 and 4+4 2
1 2 Does 2+2 = 4 26
1 3 What is 3+3 and 5+5 3
回答表:
AnswerId SessionId QuestionId Answer
1 1 1 B
2 1 1 C
3 1 2 Yes
4 1 3 A
5 1 3 C
Option_Type
OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F
.... goes up to 24 where it goes up to A-Z
25 True or False
26 Yes or No
ここのjsfiddleで、ここで見ることができるデモhtmlテーブルを作成しました
html の表をよく見ると、「不正解」列の下に不正解を表示しようとしていることがわかります。
たとえば、質問 1 の場合:
- オプション タイプは AD です
- 正解はB、C
- したがって、表に表示されるべき不正解はAとDです
私の質問は、php スクリプトで質問の option_type を選択し、可能な回答をすべて表示し、正しい回答を削除して、間違った回答のみを表示するにはどうすればよいかということです。テーブルのレイアウトは、jsfiddle のレイアウトと同じでなければなりません。
以下は、php スクリプトでのテーブル出力です。現時点では、間違った回答ではなく正しい回答を出力していることを除いて、jsfiddle と同様のテーブルが表示されています。
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.Answer, q.QuestionMarks
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $assessment);
// execute query
$stmt->execute();
// This will hold the search results
$searchQuestionId = array();
$searchQuestionContent = array();
$searchAnswer = array();
$searchMarks = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionContent, $dbAnswer, $dbQuestionMarks);
while ($stmt->fetch()) {
$searchQuestionId[] = $dbQuestionId;
$searchQuestionContent[] = $dbQuestionContent;
$searchAnswer[] = $dbAnswer;
$searchMarks[] = $dbQuestionMarks;
}
?>
</head>
<body>
<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php
echo "<table border='1' id='markstbl'>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Incorrect Answer</th>
<th class='answermarksth'>Penalty per Incorrect Answer</th>
</tr>\n";
$previous_question_id = null;
$rowspans = array_count_values($searchQuestionId);
foreach ($searchQuestionContent as $key=>$question) {
// removed logic, not necessary to set empty strings if you're skipping them
echo '<tr class="questiontd">'.PHP_EOL;
if ($previous_question_id != $searchQuestionId[$key]) {
echo '<td class="questionnumtd" name="numQuestion" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
echo '<td class="questioncontenttd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($question).'</td>' . PHP_EOL;
}
echo '<td class="answertd" name="answers[]">';
echo $searchAnswer[$key];
echo '</td>' ;
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" /></td>' . PHP_EOL;
// moved this to the end
if ($previous_question_id != $searchQuestionId[$key]) {
$previous_question_id = $searchQuestionId[$key];
}
}
echo '</tr>';
echo "</table>" . PHP_EOL;
?>
</form>
現時点では、上記のコードは、不正解ではなく、「不正解」列の下に正解を表示しています。