Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算数据库中正确答案的数量_Php_Loops_Oop - Fatal编程技术网

Php 计算数据库中正确答案的数量

Php 计算数据库中正确答案的数量,php,loops,oop,Php,Loops,Oop,我试图在数据库中计算正确答案。我有以下几行代码: $getresult = $quizAns->getAnswersByUser($_POST['user_id']); if($getresult){ $count = count($getresult); for ($x = 1; $x <= $count; $x++) { $match = $quiz->matchAnswer($getresult[$x]->question_id, $getre

我试图在数据库中计算正确答案。我有以下几行代码:

$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);
我想循环遍历每个索引并计算匹配答案的数量。但是,如果我尝试调试
$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);

}
替换为

$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($\u POST['user\u id']);
如果($getresult){
$count=count($getresult);
对于($x=1;$x匹配答案($getresult[$x]->question\u id,$getresult[$x]->ans\u id);
}
}
$counts=计数($match);

$getresult=$quizAns->getAnswersByUser($\u POST['user\u id']);
$counts=0;
如果($getresult){
$count=count($getresult);
对于($x=0;$x<$count;$x++){
$match=$quick->matchAnswer($getresult[$x]->question\u id,$getresult[$x]->ans\u id);
如果($match){
$counts+=1;
}
}
}
$counts=计数($match);
} 

GetAnswersByser的代码是什么?@NigelRen我在问题中添加了函数。你没有增加
计数
变量。它应该在循环中。检查
$this->select($args)
的结果,确保这是返回你期望的列表。转换$match[]=$quick->matchAnswer($getresult[$x]->问题id,$getresult[$x]->ans\U id);第一个选项返回0,第二个选项返回5都不正确。如果答案正确,我想增加count的值。您能检查
$this->select($args)的输出吗
如果我检查输出,ut会给出应该在数据库中执行的查询。输出是
从测验中选择*,其中id=:id和ans_id=:ans_id按测验顺序。id DESC
如果我通过转到数据库运行查询,它会给出正确的答案。
$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);
}