douduan3203 2018-07-31 17:27
浏览 219
已采纳

使用Laravel将用户的选择与数据库中的记录进行比较

I have 4 radio button and the user can choose a single choice of them, I want to check if this choice exists or not in the table question(this table contains the text of question, thei options, and the correcte answer) if it exists I want to increment the fields points Which is in the table test, and for do that the controller contains :

$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();
            }
        }

the problem is that it does not take into consideration what is in the condition if it still runs the else processing

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-07-31 18:20
    关注

    Lets revise your code a bit =)

    $userChoise = $request->answer;
    $test = new Test;
    $correctAnswers = DB::table('question')->where('correcte', $userChoise)->count();
    
    $test->nbr_points = $correctAnswers * 10;
    $test->save();
    

    Is that what you're looking for?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误