سؤال

I'm working on Question Bank System and i have problems on showing the list of question with the answer from the tables.

Click to see the table images

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:

  1. getQuestionByID()

    $select = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('q' => $this->_name), array('questionID', 'questionDesc'));
    
    $query = $select->query();
    $statement = $query->fetchAll();
    return $statement;
    
  2. 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;
    
  3. 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);
    
  4. The result from my code : click to view result from the code

Any help on this ? Thanks in advance

هل كانت مفيدة؟

المحلول

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>";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top