シンプルな投票システムを作りたい。2 つの質問と 2 つの回答があります。最初の質問には 2 つの回答がありますが、2 番目の質問には回答がありません。ある意味で私は取得したい:
First question?
Answer1
Answer2
Second question?
しかし、私は得ています
First question?
Answer1
Answer2
Second question?
Answer1
Answer2
2 番目の質問には Answer1 と Answer2 を含める必要はありません (最初の質問の回答と同じですが、mysql には 2 つの回答があります)。
そして私の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
}
}
機能:
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;
}
データ:
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.