Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 测验布局改进_Mysql_Sorting - Fatal编程技术网

Mysql 测验布局改进

Mysql 测验布局改进,mysql,sorting,Mysql,Sorting,我要做一个简单的测验。 我不想在一页纸上打印出所有问题,格式良好的布局如下: 问题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

我要做一个简单的测验。 我不想在一页纸上打印出所有问题,格式良好的布局如下:

问题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或类似的东西来改进它?

构建两个阵列,其中一个是二维阵列

比如:

保存在如下数组中:

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

然后循环它:

// 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 />";
    }
}
//循环$questions数组
foreach($qid=>$QUOTE的问题){
回声“问题:“.$quesion.”

”; //循环$answers[questionId]数组 foreach($answers[$qid]作为$answer){ echo$answer.“
”; } }

这个答案可以改进,但应该会起作用,给你一个良好的开端。

这实际上回答了我的问题:


我是舒尔Danfrom德国的解决方案成功了,如果我有时间稍微调整一下的话:)

非常感谢您的回答。但是,当我尝试您的代码时,没有打印任何内容。在没有深入解释的情况下,对代码进行错误跟踪有点困难:P您知道那里可能有什么问题吗?:)增加了解释和注释。似乎我还是无法让这个代码正常工作。非常感谢你的评论,这帮了大忙。它仍然没有打印出任何内容:/code>执行此操作时会发生什么:
while($qa=$getqa->fetch_assoc()){print_r($qa);}
如果我尝试打印$qa变量,它将返回一个包含问题的数组,每个问题只有一个答案:)
$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 />";
    }
}