gpt4 book ai didi

mysql - 测验布局改进

转载 作者:行者123 更新时间:2023-11-29 13:43:55 26 4
gpt4 key购买 nike

我要做一个简单的测验。我不想将所有问题打印在一页上,并采用格式良好的布局,例如:

问题1
答案1
答案2

问题2
答案1
答案2

我目前正在编写以下代码:

$get_qa = $mysqli->query("
SELECT
a.question AS question,
a.id AS qid,
b.answer AS answer,
b.qid AS aqid (related to: question id)
FROM rw_questions a
LEFT OUTER JOIN rw_qanswers b ON a.id = b.qid");

while($qa = $getqa->fetch_assoc()){
echo $qa['question'].$qa['answer'];
}

这将导致列表困惑。但是我如何改进这个,就像我在顶部写的那样?任何帮助都很酷!我想我需要用 foreach 或类似的东西来改进它?

最佳答案

构建 2 个数组,其中一个是二维的

喜欢:

questionId    question        answer
1 sky has color? blue
1 sky has color? red
2 what is? answer 1
....

保存在这样的数组中:

$questions[1] = "sky has color?";
$answers[1][0] = "blue";
$answers[1][1] = "red";
$questions[2] = "what is?";
$answers[2][0] = "answer 1";

php:

$questions = array();
$answers = array();

// Take every row
while($qa = $getqa->fetch_assoc()) {
// Add questions
// $question[1] = "sky has color?";
$question[$qa['qid']] = $qa['question'];

// If no answers have been set yet, init an array
if (!is_array($answers[$qa['qid']]) {
$answers[$qa['qid']] = array();
}

// Add answers
// $answers[1][] = "blue";
// $answers[1][] = "red";
$answers[$qa['qid']][] = $qa['answer'];
}

然后循环它:

// Loop $questions array
foreach ($questions as $qid => $question) {
echo "<p>Question: " . $quesion . "</p>";

// Loop $answers[questionId] array
foreach ($answers[$qid] as $answer) {
echo $answer . "<br />";
}
}

这个答案可以改进,但应该有效,并为您提供良好的启动。

关于mysql - 测验布局改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17699898/

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