我正在用Php/Laravel框架做一个简单的测验项目。当用户单击submit按钮时,我在显示用户的总得分时遇到问题。当我使用print_r()时,它完美地显示了值,但是一旦我比较它,它就返回零。
路由
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>
发布于 2018-02-09 17:05:14
这是我所做的修改,它开始工作了
<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>
https://stackoverflow.com/questions/48706114
复制