我有4个单选按钮,用户可以选择一个选项,我想检查表问题中是否存在该选项(此表包含问题,选项和更正答案的文本)如果存在,我想增加表测试中的字段点,并且为此控制器包含:
$userChoise = $request->answer;
$test = new Test;
$questions = DB::table('question')->get();
foreach ($questions as $question) {
if ($question->correcte == $userChoise) {
$test->nbr_points = 10;
$test->save();
}else{
$test->nbr_points = 0;
$test->save();
}
}
问题是,如果它仍然运行else处理,则不会考虑情况是什么
答案 0 :(得分:0)
让我们稍微修改一下代码=)
$userChoise = $request->answer;
$test = new Test;
$correctAnswers = DB::table('question')->where('correcte', $userChoise)->count();
$test->nbr_points = $correctAnswers * 10;
$test->save();
这是您要找的吗?