我想计算从表单输入的选项a / b / c中的分数。对于给定的每个输入,值从= 1,b = 2,c = 3开始,计数后我想将结果保存在得分变量中
在我的控制器中
public function score($id) {
$user_login = Auth::user()->id;
$answers = Answer::select('user_answer')->where('jenis_quiz_id','=',$id)->where('user_id','=',$user_login)->get();
static $a = 0;
static $b = 0;
static $c = 0;
static $score = 0;
if($answers->count()) {
foreach ($answers as $answer) {
if ($answer->user_answer == '1') {
$a++;
} else if($answer->user_answer == '2') {
$b++;
} elseif($answer->user_answer == '3') {
$c++;
}
}
}
$score = $a+$b+$c;
$returnScore = $score;
$a = null;
$b = null;
$c = null;
$score = null;
return $returnScore;
}
public function getShowResultOfQuiz($id) {
$categoryquiz = JenisQuiz::findOrFail($id);
$user = Auth::user()->id;
$score= $this->score($id);
$kelas = Auth::user()->kelas;
$instansi = Auth::user()->instansi;
History::create([
'user_id'=>$user,
'jenis_quiz_id'=>$id,
'score'=> $score,
'kelas' => $kelas,
'instansi' => $instansi
]);
// $time_taken = date("H:i:s", strtotime(Answer::whereJenisQuizId($id)->orderBy('id', 'desc')->first()->time_taken));
switch ($id) {
case '1':
return view('quiz1',compact('score','categoryquiz'));
case '2':
return view('quiz2',compact('score','categoryquiz'));
case '3':
return view('quiz3',compact('score','categoryquiz'));
}
}
我想重置每个提交的输入的a,b,c和分数变量,问题是总是从先前提交的答案中添加分数,而不是在再次计算分数之前将其重置,请帮助我,谢谢