基本上我正在实施MCQ问题。一切都很简单。单一表格,包含所有问题(从数据库中提取)和选项。
我被困在需要检查用户选择的答案是否正确的地方。
有一个单独的数据库,其中包含与所有问题相对应的正确选项 喜欢
array(q_1 => 3,
q_2 => 2,
q_3 => 4,
); //just an example
代码:
这是一次失败的尝试
public function checkAnswers($submition){
//compare array with db right answers
$correct = array();
foreach ($submition as $attempt){
$x = 0;
if ($attempt == $this->_db->get('answers', array('q_id','=','1')){
$correct[$x] = "yes";
}
}
return $correct;
//$this->displayFinalScore()
}
这就是我收到提交的方式
$score->checkAnswers(array(
'question1' => Input::get('question1'),
'question2' => Input::get('question2'),
'question3' => Input::get('question3'),
'question4' => Input::get('question4'),
'question5' => Input::get('question5'),
'time' => 300,
)));
请在投票之前告诉我是否需要更多代码来帮助我。 感谢。
感谢您对安德鲁的帮助,但我采取了不同的方法并弄明白了。这是答案:
public function checkAnswers($submition){
//compare array with db right answers
$answers = $this->getAnswerKey();
//$answer->q_id == question id & $asnwer->option_id == correct answer
//submittion for each question in loop is $submition[$question->q_id]
foreach ($answers as $answer) {
if($submition[$answer->q_id] == $answer->option_id){
$this->score = $this->score + 4;
}
}
$this->createEntry($this->score, $submition['user_id']);
}
希望这会帮助有需要的人!