Birlikisgu
Birlikisgu

Reputation: 329

Loop in loop when printing datas from mysql

I want to create simple poll system. I have two question and two answers. First Question has two answer but second question hasn't any answer. In a sense I want to get:

First question?
Answer1
Answer2

Second question?

But I'm getting

First question?
Answer1
Answer2

Second question?
Answer1 
Answer2

Second question need not to have Answer1 and Answer2(same with First question answers but I have two answer in mysql).

And my foreach loops.. How need i to change my loops?

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
    }
}

Functions:

public function getQuestion( $cat )
{
    if(empty($cat))
    {
        header("location:index.php");
    }

    $this->sql = "SELECT * FROM questions WHERE category='" . $cat . "'";
    $data = parent::getData();
    return $data;
}

public function getAnswer( $question )
{
    $this->sql = "SELECT * FROM answers WHERE questions='" . $question .  "'";
    $data = parent::getData();
    return $data;
}

Datas:

  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
        )

)

note: these datas are only example. These are data that in mysql.

Upvotes: 0

Views: 66

Answers (1)

tereško
tereško

Reputation: 58454

The easiest way i can think of would be to have two queries:

  1. requesting list of question form database ordered by question_id
  2. requesting lost of all answers for all the question from list 1. ordered by question_id

Then on the php side of things, you match the answers to each question. It will be a single loop, since both question and answer are already ordered and there is no need to "step back".

What you will end up will be a structure like this:

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"
       ]
    }
]

You should avoid querying DB for answers to each question separately. It's kinda wasteful.

Upvotes: 2

Related Questions