Reputation: 15
I'm working on Question Bank System and i have problems on showing the list of question with the answer from the tables.
I want to show the result like this:
$question = array{
array {
'question' => 'Question MCQ',
'answer' => array{
'answer 1',
'answer 2',
'answer 3',
'answer 4',
},
'correct_answer' = 0
},
array {
'question' => 'Question MCA',
'answer' => array{
'answer 1',
'answer 2',
'answer 3',
'answer 4',
},
'correct_answer' = 2
},
array {
'question' => 'Question True and False',
'answer' => array{
'True',
'False',
},
'correct_answer' = 1
},
}
The code that im working on right now is this:
getQuestionByID()
$select = $this->select()
->setIntegrityCheck(false)
->from(array('q' => $this->_name), array('questionID', 'questionDesc'));
$query = $select->query();
$statement = $query->fetchAll();
return $statement;
getMcQ()
$select = $this->select()
->setIntegrityCheck(false)
->from(array('q' => 'questions'), array('questionID', 'questionDesc'))
->join(array('aq' => 'qanswer'), 'q.questionID = aq.questionID')
->join(array('a' => 'answers'), 'aq.answerID = a.answerID', array('answerID', 'answerDesc', 'isAnswer'));
$query = $select->query();
$stamnt = $query->fetchAll();
return $stamnt;
Controller
$getQ = new Questions();
$res = $getQ->getQuestionByID();
$questions = array();
foreach ($res as $que) {
$tmp['question'] = $que->questionDesc;
$res_ans = $getQ->getMcq();
$index = 0;
foreach ($res_ans as $ans) {
$tmp['answer'] = $ans->answerDesc;
if ($ans->isAnswer == 1) {
$tmp['correct_answer'] = $index;
}
$index++;
}
array_push($questions, $tmp);
}
echo "<pre>";
print_r($questions);
The result from my code : click to view result from the code
Any help on this ? Thanks in advance
Upvotes: 1
Views: 51
Reputation: 5772
I did not understand what you want to do with 'correct_answer'
but I think that replacing your controller code by this, it should help you (except maybe the field 'correct_answer'
but the principle will the same for the rest).
$getQ = new Questions();
$res = $getQ->getQuestionByID();
$res_ans = $getQ->getMcq();
$questions = array();
foreach ($res as $que) {
$tmp = array();
$tmp['question'] = $que->questionDesc;
$index = 0;
foreach ($res_ans as $ans) {
if ($ans->questionID == $que->questionID){
$tmp['answer'][] = $ans->answerDesc;
if ($ans->isAnswer == 1) {
$tmp['correct_answer'] = $index;
}
$index++;
}
}
array_push($questions, $tmp);
}
echo "<pre>";
print_r($questions);
echo "</pre>";
Upvotes: 0