douduan3203 2018-07-31 09:27
浏览 221
已采纳

使用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 10: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?

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部