我具有低於此查詢:如何在HTML表格顯示在不同行中的答案
SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS 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 = "GHWSW1" AND q.QuestionId = 1
GROUP BY an.SessionId, an.QuestionId
ORDER BY q.QuestionId, an.Answer
以上查詢輸出低於這個結果:
SessionId SessionName QuestionId QuestionContent Answer QuestionMarks
1 GHWSW1 1 Here are 2 answers BD 5
所以我包含在代碼查詢下方設置了一個HTML表:
$assessment = "GHWSW1";
$number = 1;
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent,
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS 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 = ? AND q.QuestionId = ?
GROUP BY an.SessionId, an.QuestionId
ORDER BY q.QuestionId, an.Answer
";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("si", $assessment, $number);
// 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'>Answer</th>
<th class='noofmarksth'>Total Marks</th>
</tr>\n";
foreach ($searchQuestionContent as $key=>$question) {
echo '<tr class="questiontd">'.PHP_EOL;
echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">'.htmlspecialchars($searchAnswer[$key]).'</td>' ;
echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
echo "</table>" . PHP_EOL;
?>
所以HTML表顯示它是這樣的:
QuestionId QuestionContent Answer QuestionMarks
1 Here are 2 answers BD 5
但我想它像這樣在HTML表:
QuestionId QuestionContent Answer QuestionMarks
1 Here are 2 answers B 5
D
第二個答案D
應該是在一個單獨的行和其他行應該有一個行跨度。但是,這怎麼能實現呢?
會話表:
SessionId (auto) SessionName
1 AAA
問表:
SessionId QuestionId (auto) QuestionContent
1 1 What is 2+2?
回答表:
AnswerId (auto) SessionId QuestionId Answer
1 1 1 B
2 1 1 D
你不能,因爲你在查詢做GROUP_CONCAT,你正在破壞你所需要的信息來表明需要一個新的行。你可以使用'
'作爲分隔符。這會給你多行,但仍然只是一個單元格,節省你不得不計算rowspans。 –
這就是我所說的:一個很好的格式化!而且,你的MySQL查詢真棒! (對不起,我只在MySQL中有一些小知識) –
@MarcB沒關係,我弄明白了。需要擺脫GROUP CLAUSE,然後在foreach循環中包含一些代碼,以便它將顯示行中需要空白的空白信息。 – user1794342