我正在尝试计算数据库中的正确答案。我有以下几行代码:
$getresult = $quizAns->getAnswersByUser($_POST['user_id']);
if($getresult){
$count = count($getresult);
for ($x = 1; $x <= $count; $x++) {
$match = $quiz->matchAnswer($getresult[$x]->question_id, $getresult[$x]->ans_id);
}
}
$counts = count($match);
在$getresult
中,我收到用户提交的答案的数量,该答案必须始终为4,如下所示:
Array
(
[0] => stdClass Object
(
[id] => 220
[user_id] => 84
[question_id] => 43
[answer_id] => 31
)
[1] => stdClass Object
(
[id] => 219
[user_id] => 84
[question_id] => 48
[answer_id] => 53
)
[2] => stdClass Object
(
[id] => 218
[user_id] => 84
[question_id] => 49
[answer_id] => 56
)
[3] => stdClass Object
(
[id] => 217
[user_id] => 84
[question_id] => 50
[answer_id] => 62
)
)
我想遍历每个索引并计算匹配答案的数量。但是,如果我尝试调试$counts
,我只会得到1。我希望有4个或3个,但不会只有一个。以下代码用于功能匹配答案:
public function matchAnswer($question_id, $ans_id){
$args = array(
'where' => array(
'id' => $question_id,
'ans_id' => $ans_id
)
);
return $this->select($args);
}
这是getAnswersByUser
的功能:
public function getAnswersByUser($id, $is_die = false){
$args = array(
'where' => array(
'user_id' => $id
)
);
return $this->select($args);
}
答案 0 :(得分:1)
替换为
$getresult = $quizAns->getAnswersByUser($_POST['user_id']);
if($getresult){
$count = count($getresult);
for ($x = 1; $x <= $count; $x++) {
$match = $quiz->matchAnswer($getresult[$x]->question_id,$getresult[$x]->ans_id);
}
}
$counts = count($match);
与
$getresult = $quizAns->getAnswersByUser($_POST['user_id']);
$counts = 0;
if($getresult){
$count = count($getresult);
for ($x = 0; $x < $count; $x++) {
$match = $quiz->matchAnswer($getresult[$x]->question_id, $getresult[$x]->ans_id);
if($match){
$counts += 1;
}
}
}
$counts = count($match);
}