Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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
Php 从mysql打印数据时循环中的循环_Php_Foreach - Fatal编程技术网

Php 从mysql打印数据时循环中的循环

Php 从mysql打印数据时循环中的循环,php,foreach,Php,Foreach,我想创建一个简单的投票系统。我有两个问题和两个答案。第一个问题有两个答案,但第二个问题没有答案。从某种意义上说,我想得到: First question? Answer1 Answer2 Second question? 但是我越来越 First question? Answer1 Answer2 Second question? Answer1 Answer2 第二个问题不需要有答案1和答案2(与第一个问题的答案相同,但我在mysql中有两个答案) 我的foreach循环。。我需要如

我想创建一个简单的投票系统。我有两个问题和两个答案。第一个问题有两个答案,但第二个问题没有答案。从某种意义上说,我想得到:

First question?
Answer1
Answer2

Second question?
但是我越来越

First question?
Answer1
Answer2

Second question?
Answer1 
Answer2
第二个问题不需要有答案1和答案2(与第一个问题的答案相同,但我在mysql中有两个答案)

我的foreach循环。。我需要如何更改循环

foreach($questions->getQuestion($_GET["category"]) as $data) // Questions
{
    echo $data["question"] . "<br/>";
    foreach($questions->getAnswer($data["id"]) as $answers) // Answers
    {
        echo $answers["answer"] . "<br/>"; // This needn't print data to below of Second Question
    }
}
数据:

  Array
(
    [0] => Array
        (
            [id] => 1
            [soru] => First Question
            [kategori] => 1
        )

    [1] => Array
        (
            [id] => 2
            [soru] => Second Question
            [kategori] => 1
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [soru] => First Question
            [kategori] => 1
        )

    [1] => Array
        (
            [id] => 2
            [soru] => Second Question
            [kategori] => 1
        )

    [2] => Array
        (
            [id] => 1
            [cevap] => Answer1
            [sorular] => 1
        )

    [3] => Array
        (
            [id] => 2
            [cevap] => Answer2
            [sorular] => 1
        )

    [4] => Array
        (
            [id] => 1
            [cevap] => Answer1
            [sorular] => 1
        )

    [5] => Array
        (
            [id] => 2
            [cevap] => Answer2
            [sorular] => 1
        )

)

注:这些数据仅为示例。这些是mysql中的数据。

我能想到的最简单的方法是有两个查询:

  • question\u id
  • 请求丢失列表1中所有问题的所有答案。按问题编号排序
  • 然后在php方面,匹配每个问题的答案。这将是一个单循环,因为问题和答案都已排序,不需要“后退”

    最终的结果将是这样一个结构:

    list = [
        {
           question: "Lorem ipsum",
           answers: [
              "answer number one",
              "answer number two"
           ]
        },
        {
           question: "Cogito ergo sum",
           answers: [
              "answer number one",
              "answer number two"
              "answer number three",
              "answer number four"
           ]
        }
    ]
    

    您应该避免单独向DB查询每个问题的答案。这有点浪费。

    @tereško现在?你能理解吗?你的示例代码看起来不错。。如果一个问题没有答案,它们就不会显示出来。你有什么问题?您是否要求人们编写getQuestion和getAnswer方法?@Birlikisgu,那么您必须更改“SQL代码”。否则就没有意义了。@Birlikisgu,没有什么可看的,只需重写SQL,使问题查询
    连接
    s问题,对于类别中的所有问题,按
    question\u id
    排序。或者更好——制定一个方法
    getQuestions($cat)
    。如果你不会写作,那就雇佣一个会写的人。StackOverflow不是免费的代码生成器。
    list = [
        {
           question: "Lorem ipsum",
           answers: [
              "answer number one",
              "answer number two"
           ]
        },
        {
           question: "Cogito ergo sum",
           answers: [
              "answer number one",
              "answer number two"
              "answer number three",
              "answer number four"
           ]
        }
    ]