mysql - Quiz layout improvement -
i'm simple quiz. wan't print questions out in 1 page, formatted layout like:
question 1
answer 1
answer 2
question 2
answer 1
answer 2
i'm working on following code:
$get_qa = $mysqli->query(" select a.question question, a.id qid, b.answer answer, b.qid aqid (related to: question id) rw_questions left outer join rw_qanswers b on a.id = b.qid"); while($qa = $getqa->fetch_assoc()){ echo $qa['question'].$qa['answer']; }
this result in messy list. how improve like, wrote @ top? cool! guess need improve foreach or that?
build 2 arrays, 1 of them 2-dimensional
like:
questionid question answer 1 sky has color? blue 1 sky has color? red 2 is? answer 1 ....
save in array this:
$questions[1] = "sky has color?"; $answers[1][0] = "blue"; $answers[1][1] = "red"; $questions[2] = "what is?"; $answers[2][0] = "answer 1";
php:
$questions = array(); $answers = array(); // take every row while($qa = $getqa->fetch_assoc()) { // add questions // $question[1] = "sky has color?"; $question[$qa['qid']] = $qa['question']; // if no answers have been set yet, init array if (!is_array($answers[$qa['qid']]) { $answers[$qa['qid']] = array(); } // add answers // $answers[1][] = "blue"; // $answers[1][] = "red"; $answers[$qa['qid']][] = $qa['answer']; }
then loop it:
// loop $questions array foreach ($questions $qid => $question) { echo "<p>question: " . $quesion . "</p>"; // loop $answers[questionid] array foreach ($answers[$qid] $answer) { echo $answer . "<br />"; } }
this answer can improved should work , give kickstart.
Comments
Post a Comment