i want to count my score from option a/b/c that inputted from a form. for each input given, value from a =1,b=2,c=3 ,after count i want to save the result in score variable
in my controller
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'));
}
}
i want to reset a,b,c,and score variables every input submitted,and the problem is score always added from previous submitted answer instead of reset it before calculating the score again, help me , thanks