1
我如何在我的PHP代碼顯示正確的表格佈局上的問題:想在HTML表中的數據正確顯示
我想在一個表中顯示的答案和他們的文本輸入框。現在目前它顯示如下:
QuestionNo Question Answer Marks Per Answer
1 What is 2+2 B (text input)
2 Name the 3 hobbits? BCE (text input)
我想更改表的顯示,使得它看起來像下面這樣:
QuestionNo Question Answer Marks Per Answer
1 What is 2+2? B (text input)
2 Name the 3 Hobbits? B (text input)
C (text input)
E (text input)
- 正如您可以從新顯示中看到的那樣。我希望每個問題的每個答案都顯示在他自己的行中,而不是每個問題中的每個問題的答案都在一行中,而這正是它現在正在做的事情。
- 我想要的文字輸入也可以在自己的行顯示,像答案:
我的問題是,如何可以點1和2來實現,以便它可以適應新的佈局?
下面是當前顯示的代碼:
<?php
$assessment = $_SESSION['id'] . $sessionConcat;
include('connect.php');
$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 = ?
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("s", $assessment);
// execute query
$stmt->execute();
// This will hold the search results
$searchQuestionId = array();
$searchQuestionContent = array();
$searchAnswer = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbQuestionId, $dbQuestionContent, $dbAnswer);
while ($stmt->fetch()) {
$searchQuestionId[] = $dbQuestionId;
$searchQuestionContent[] = $dbQuestionContent;
$searchAnswer[] = $dbAnswer;
}
?>
</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='answermarksth'>Marks per Answer</th>
<th class='noofmarksth'>Total Marks</th>
</tr>\n";
$previous_question_id = null;
foreach ($searchQuestionContent as $key=>$question) {
if ($previous_question_id == $searchQuestionId[$key]) {
$searchQuestionId[$key] = '';
$question = '';
}else{
$previous_question_id = $searchQuestionId[$key];
}
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">';
echo $searchAnswer[$key];
echo '</td>' ;
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
echo "</table>" . PHP_EOL;
?>
</form>
</body>
下面是會議,問題和回答表的樣子:
會話表:
SessionId (auto) SessionName
1 AAA
2 AAB
問表:
SessionId QuestionId (auto) QuestionContent
1 1 What is 2+2?
1 2 Name the 3 hobbits?
回答表:
AnswerId (auto) SessionId QuestionId Answer
1 1 1 B
2 1 2 B
3 1 2 C
4 1 2 E
我已經試過你的答案,你好心給我,但我怕它沒有工作。在HTML表格的Answer欄中,它將問題1的答案顯示爲「BB」,而它只應該是「B」,對於問題2,它顯示的答案也是「BB」,當它應該是「BCE」時。它是由兩個問題顯示一個答案,並在這兩個問題顯示出來,如果這是有道理的 – user1794342
我更新的問題表,使其具有ncluded你的foreach循環 – user1794342
更換'的foreach($ searchAnswer爲$鍵=> $回答){回聲「 $ answer
「; }'的'回聲$ searchAnswer [$關鍵]' – air4x