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.