gpt4 book ai didi

php - 如何在每一行中显示答案和文本输入

转载 作者:行者123 更新时间:2023-11-30 23:26:14 25 4
gpt4 key购买 nike

我对如何在我的 php 代码中显示正确的表格布局有疑问:

我想在表格中显示答案及其文本输入。现在它显示如下:

 Question             Answer        Marks Per Answer     
What is 2+2 B (text input)
Name the 3 hobbits? BCE (text input)

我想更改表格的显示,使其如下所示:

 Question             Answer        Marks Per Answer 
What is 2+2? B (text input)
Name the 3 Hobbits? B (text input)
C (text input)
E (text input)
  1. 正如您从新显示中看到的那样。我希望每个问题的每个答案都显示在他们自己的行中,而不是每个问题的所有答案都显示在一行中,这正是它目前正在做的。
  2. 我希望文本输入也显示在自己的行中,例如答案:

我的问题是第1点和第2点如何实现才能匹配新布局?

下面是当前显示的代码:

$assessment = $_SESSION['id'] . $sessionConcat;

include('connect.php');

$query = "SELECT q.QuestionId, q.QuestionContent, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer,
FROM Question q
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
WHERE s.SessionName = ?
";

// 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()) {
$searchQuestionContent[] = $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</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
</tr>\n";

foreach ($searchQuestionContent as $key=>$question) {
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">'.htmlspecialchars($searchAnswer).'</td>' . PHP_EOL;
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
}
echo "</table>" . PHP_EOL;

?>

</form>

</body>

下面是问题表的样子:

问题表:

QuestionId (auto)  QuestionContent
1 What is 2+2?
2 Name the 3 hobbits?

答案表:

AnswerId (auto)  QuestionId   Answer
1 1 B
2 2 B
3 2 C
4 2 E

最佳答案

看起来您可以去掉分组子句。您可能希望按问题 ID 排序,这样同一问题的所有答案都会一起给出。

SELECT q.QuestionId, q.QuestionContent, an.Answer
FROM Question q
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer

关于php - 如何在每一行中显示答案和文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com