0

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.

2
  • @tereško now? can you understand? Commented May 30, 2012 at 18:42
  • Your example code looks fine.. if there are no answers to a question, they wouldn't display. What issue are you having? Are you asking people to write the getQuestion and getAnswer method? Commented May 30, 2012 at 18:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Birlikisgu , then you will have to change the "SQL code". Otherwise it will make no sense.
@Birlikisgu , there is nothing to look, just rewrite the SQL so that the questions query JOINs the questions , for all question in the category, ordered by question_id. Or even better - make a method getQuestions( $cat ).
If you cannot write, then hire someone who can. StackOverflow is not a free code generator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.