Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop in loop when printing datas from mysql
    primarykey
    data
    text
    <p>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:</p> <pre><code>First question? Answer1 Answer2 Second question? </code></pre> <p>But I'm getting</p> <pre><code>First question? Answer1 Answer2 Second question? Answer1 Answer2 </code></pre> <p>Second question need not to have Answer1 and Answer2(same with First question answers but I have two answer in mysql).</p> <p>And my foreach loops.. How need i to change my loops?</p> <pre><code>foreach($questions-&gt;getQuestion($_GET["category"]) as $data) // Questions { echo $data["question"] . "&lt;br/&gt;"; foreach($questions-&gt;getAnswer($data["id"]) as $answers) // Answers { echo $answers["answer"] . "&lt;br/&gt;"; // This needn't print data to below of Second Question } } </code></pre> <p>Functions:</p> <pre><code>public function getQuestion( $cat ) { if(empty($cat)) { header("location:index.php"); } $this-&gt;sql = "SELECT * FROM questions WHERE category='" . $cat . "'"; $data = parent::getData(); return $data; } public function getAnswer( $question ) { $this-&gt;sql = "SELECT * FROM answers WHERE questions='" . $question . "'"; $data = parent::getData(); return $data; } </code></pre> <p>Datas:</p> <pre><code> Array ( [0] =&gt; Array ( [id] =&gt; 1 [soru] =&gt; First Question [kategori] =&gt; 1 ) [1] =&gt; Array ( [id] =&gt; 2 [soru] =&gt; Second Question [kategori] =&gt; 1 ) ) Array ( [0] =&gt; Array ( [id] =&gt; 1 [soru] =&gt; First Question [kategori] =&gt; 1 ) [1] =&gt; Array ( [id] =&gt; 2 [soru] =&gt; Second Question [kategori] =&gt; 1 ) [2] =&gt; Array ( [id] =&gt; 1 [cevap] =&gt; Answer1 [sorular] =&gt; 1 ) [3] =&gt; Array ( [id] =&gt; 2 [cevap] =&gt; Answer2 [sorular] =&gt; 1 ) [4] =&gt; Array ( [id] =&gt; 1 [cevap] =&gt; Answer1 [sorular] =&gt; 1 ) [5] =&gt; Array ( [id] =&gt; 2 [cevap] =&gt; Answer2 [sorular] =&gt; 1 ) ) </code></pre> <p><em>note: these datas are only example. These are data that in mysql.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload