Note that there are some explanatory texts on larger screens.

plurals
  1. POArrays operiation
    primarykey
    data
    text
    <p>How to move in order to fit the answers to questions in array or how to make sql query for the desired effect? I tired of this alredy a few days.</p> <pre><code>&lt;?php public function getQuestions() { $sql = 'SELECT * FROM questions'; $result = Database::query($sql); while ($row = mysql_fetch_assoc($result)) { $questions[] = $row; } return $questions; } public function getAnswers($qid) { $sql = 'SELECT idq, answer, truth FROM answers WHERE idq = '.$qid; $result = Database::query($sql); while ($row = mysql_fetch_assoc($result)) { $answers[] = $row; } return $answers; } public static function array_add($a1, $a2) { $aRes = $a1; foreach (array_slice(func_get_args(), 1) as $aRay) { foreach (array_intersect_key($aRay, $aRes) as $key =&gt; $val) $aRes[$key] += $val; $aRes += $aRay; } return $aRes; } public static function getQuiz() { $db = new Database(); self::$questions = $db-&gt;getQuestions(); foreach( self::$questions as $q ) { $qid = $q['qid']; self::$answers[$qid] = $db-&gt;getAnswers($qid); } $s = Quiz::array_add(self::$questions, self::$answers); print "&lt;pre&gt;"; print_r($s); print "&lt;/pre&gt;"; } ?&gt; </code></pre> <p>output:</p> <pre><code>Array ( [0] =&gt; Array ( [qid] =&gt; 1 [question] =&gt; Bunty szlachty pod has?ami obrony praw nazywamy? ) [1] =&gt; Array ( [qid] =&gt; 2 [question] =&gt; Kto dowodzi? wojskami kozackimi podczas powstania Chmielnickiego? [0] =&gt; Array ( [idq] =&gt; 1 [answer] =&gt; liberum veto [truth] =&gt; 1 ) [1] =&gt; Array ( [idq] =&gt; 1 [answer] =&gt; jurydyki [truth] =&gt; 0 ) [2] =&gt; Array ( [idq] =&gt; 1 [answer] =&gt; rokosze [truth] =&gt; 0 ) ) [2] =&gt; Array ( [qid] =&gt; 3 [question] =&gt; W którym roku odby?a si?, morska, bitwa pod Oliw?? [0] =&gt; Array ( [idq] =&gt; 2 [answer] =&gt; Bohdan Chmielnicki [truth] =&gt; 1 ) [1] =&gt; Array ( [idq] =&gt; 2 [answer] =&gt; Gustaw II Adolf [truth] =&gt; 0 ) [2] =&gt; Array ( [idq] =&gt; 2 [answer] =&gt; Iwan IV Gro?ny [truth] =&gt; 0 ) ) [3] =&gt; Array ( [0] =&gt; Array ( [idq] =&gt; 3 [answer] =&gt; 1627 [truth] =&gt; 1 ) [1] =&gt; Array ( [idq] =&gt; 3 [answer] =&gt; 1608 [truth] =&gt; 0 ) [2] =&gt; Array ( [idq] =&gt; 3 [answer] =&gt; 1654 [truth] =&gt; 0 ) ) ) </code></pre> <p>I tried this solution yet but I need to have answers separately:</p> <pre><code>function get_quiz() { $sql = 'SELECT question, GROUP_CONCAT(answer ORDER BY answer SEPARAtoR " ") as answers FROM questions LEFT JOIN answers ON (questions.qid=answers.idq) GROUP BY answers.idq;'; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $quiz[] = $row; } return $quiz; } </code></pre> <p>output:</p> <pre><code>Array ( [0] =&gt; Array ( [question] =&gt; Bunty szlachty pod has?ami obrony praw nazywamy? [answers] =&gt; jurydyki liberum veto rokosze ) [1] =&gt; Array ( [question] =&gt; Kto dowodzi? wojskami kozackimi podczas powstania Chmielnickiego? [answers] =&gt; Bohdan Chmielnicki Gustaw II Adolf Iwan IV Gro?ny ) [2] =&gt; Array ( [question] =&gt; W którym roku odby?a si?, morska, bitwa pod Oliw?? [answers] =&gt; 1608 1627 1654 ) ) </code></pre>
    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