drqyxkzbs21968684 2019-07-01 19:02
浏览 22

如何在数据库中首次尝试测试和第二次尝试相同的测验得到答案

So, I have created a quiz with the answer options a, b, c and I want to get an answer from that option and save it to the database. How to save in the database so that the answer from the first try of the quiz is different from the second trial answer from the quiz, so that I can get the answer and call in the answer to be calculate in score function of the quiz?

what i have tried :

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();//get the answer

            unset($a);
            unset($b);
            unset($c);
            $a = 0;
            $b = 0;
            $c = 0;
            if($answers->count()) {
                foreach ($answers as $answer) {
                    switch ($answer->user_answer) {
                        case 1:
                        $a++;
                        break;
                        case 2:
                        $b++;
                        break;
                        case 3:
                        $c++;   
                        break;
                    }

                        }

                        return $a+$b+$c;



                    }
                    public function getShowResultOfQuiz($id){
                        $categoryquiz= JenisQuiz::findOrFail($id);
                        $user= Auth::user()->id;
                        // $instansi = Auth::user()->instansi;
                        // $kelas = Auth::user()->kelas;

                        $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('user.pages.quizresult',compact('score','categoryquiz'));
                                break;

                                case '2':
                                return view('user.pages.quizanxietyresult',compact('score','categoryquiz'));
                                break;

                                case '3':
                                return view('user.pages.quizdepresiresult',compact('score','categoryquiz'));
                                break;
                            }

                        }

                    }

problem : can't distinguish answers from the first try of quiz trial and second try of quiz (from same quiz form but i taken it twice, and how to make the difference between it.

My code result above is score from first try added to the second try,how to not added it??????

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算