如何输出我的 LaravelQuiz 应用程序的总分

How to output the totalScore of my LaravelQuiz App

我正在使用 Php/Laravel 框架开发一个简单的测验项目。当用户单击提交按钮时,我无法显示用户的总分。当我使用 print_r() 它完美地显示了值但是一旦我比较它,它 returns 零。

路线

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('profile', 'UserController@profile')->middleware('auth');
Route::post('profile', 'UserController@update_avatar')->middleware('auth');
Route::get('quiz', 'UserController@quiz');
Route::post('check', 'UserController@check');

Route::get('/home', 'HomeController@index')->name('home');

Quiz.blade.php(这是问答页面)

<form class="form-horizontal" method="POST" action="check">
    @foreach($quiz as $quizs)
        <div class="panel-body">
            {{ csrf_field() }}
            <h3>{{ $quizs->id}}. {{ $quizs->question }}</h3>
            <div class="btn-group btn-group-toggle" data-toggle="buttons">
                <label class="btn btn-primary">
                    <input type="radio" name="question_{{ $quizs->id }}_answer" id="question_{{ $quizs->id }}_answer_A" value="A" autocomplete="off"> {{ $quizs->first_opt }}
                </label>
                <label class="btn btn-primary">
                    <input type="radio" name="question_{{ $quizs->id }}_answer"  id="question_{{ $quizs->id }}_answer_B" value="B" autocomplete="off"> {{ $quizs->second_opt }}
                </label>
                <label class="btn btn-primary">
                    <input type="radio" name="question_{{ $quizs->id }}_answer" id="question_{{ $quizs->id }}_answer_C" value="C" autocomplete="off"> {{ $quizs->third_opt }}
                </label>
            </div>
            </br></br>
        </div>
    @endforeach
    <!-- <a type="button" href="#" class="btn btn-outline-primary pull-right">Submit Question</a> -->
    <div class="text-center">
        <input type="submit" class="btn btn-primary" style="text-align: center;" value="Submit Question" />
    </div>
</form>

控制器

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use Auth;
  use Image;
  use File;
  use App\Quiz as Quiz;
  use App\User as User;

  class UserController extends Controller
  {

      public function profile()
      {
          return view('profile', array('user' => Auth::user()));
      }


      public function quiz()
      {
          $quiz = Quiz::get();

          return view('quiz', compact('quiz'));
      }

      public function check(Request $request)
      {
          $ans = $request->all();
          //print_r($ans);
          return view('check', compact('ans'));
      }
  }

Check.blade.php(这是用户打分的地方)

<?php 

  foreach ($ans as $key => $value)
  {
      $names[] = $key;
  }


  $answer1 =  $names[1];
  $answer2 =  $names[2];
  $answer3 =  $names[3];
  $answer4 =  $names[4];

  $totalCorrect = 0;

  if($answer1 == 'B') { $totalCorrect++; }
      if($answer2 == 'A') { $totalCorrect++; }
          if($answer3 == 'B') { $totalCorrect++; }
              if($answer4 == 'C') { $totalCorrect++; }
                  $quiz = App\Quiz::all();

                  $question1 = $quiz[0]->question;
                  $question2 = $quiz[1]->question;
                  $question3 = $quiz[2]->question;
                  $question4 = $quiz[3]->question;
?>

<h2 style="text-align: center;">Quiz Result</h2>

<br>

<h4 style="text-align: center;color: green">You answered {{$totalCorrect}} / 4  Answered Correctly</h4> 

<br>

@if($answer1 != 'B' || $answer2 != 'A' || $answer3 != 'B' || $answer4 != 'C')
    <h4 style="text-align: center;color: red">Failed Question/Answer</h4>
@endif


<center>
@if($answer1 != 'B')
    {{$question1}} <br> <h5 style="text-align: center;color: green">The Correct Option is B</h5> <br> @endif
<br>

@if($answer2 != 'A')
    {{$question2}} <br> <h5 style="text-align: center;color: green">The Correct Option is A</h5> <br> @endif
<br>

@if($answer3 != 'B')
    {{$question3}} <br> <h5 style="text-align: center;color: green">The Correct Option is B</h5> <br> @endif
<br>

@if($answer4 != 'C')
    {{$question4}} <br> <h5 style="text-align: center;color: green">The Correct Option is C</h5> <br> @endif

<a class="btn btn-primary" href="quiz" role="button">Try Again</a>

</center>

这是我所做的修改,它开始工作了

  <form class="form-horizontal" method="POST" action="check">
                @foreach($quiz as $quizs)
                <div class="panel-body">
                        {{ csrf_field() }}
                         <h3> {{ $quizs->question }}</h3>
                         <div class="btn-group btn-group-toggle" data-toggle="buttons">
                              <label class="btn btn-primary">
                                 <input type="radio" name="question_{{ $quizs->id }}_answer" value="A" autocomplete="off"> {{ $quizs->first_opt }}
                              </label>
                              <label class="btn btn-primary">
                                 <input type="radio" name="question_{{ $quizs->id }}_answer"  id="question_{{ $quizs->id }}_answer_B" value="B" autocomplete="off"> {{ $quizs->second_opt }}
                              </label>
                              <label class="btn btn-primary">
                                <input type="radio" name="question_{{ $quizs->id }}_answer" id="question_{{ $quizs->id }}_answer_C" value="C" autocomplete="off"> {{ $quizs->third_opt }}
                              </label>
                        </div>
                    </br></br>
                </div>
                 @endforeach
                 <!-- <a type="button" href="#" class="btn btn-outline-primary pull-right">Submit Question</a> -->
                 <div class="text-center">
                    <input type="submit" class="btn btn-primary" style="text-align: center;" value="Submit Question" />
                 </div>
       </form>