Php Laravel web应用程序显示不正确的数据库条目
我正在尝试对一个由前员工创建的web应用程序进行故障排除,该应用程序无法100%正常运行。我对PHP有一个初学者的理解 这是一个相对简单的应用程序,显示过去的考试文件,但对公众是锁定的。它在大多数情况下都正常工作,除了在样例应答文件中发生了一件奇怪的事情。并不是每个考试都有一个答案样本文件。没有的应该是空白的。但是,发生的情况是,一些没有相应答案文件的考试文件正在显示其他考试的考试文件按钮 你可以从这张图片中看到,1997年春季的第一次考试显示了一个空白区域,用于填写正确的样本答案。接下来的三次考试也是正确的,因为它们显示的是正确的答案文件,然而,最后三次考试实际上在数据库中没有答案样本文件,而是显示了2004年春季的答案样本,就像它们在重复特定序列中的最后一个文件名一样 我认为正在发生的事情是,代码中没有明确说明如果没有相应的应答文件,则渲染一个空格,但我不知道实际情况是否如此。这是index.blade.php中的代码:Php Laravel web应用程序显示不正确的数据库条目,php,laravel,Php,Laravel,我正在尝试对一个由前员工创建的web应用程序进行故障排除,该应用程序无法100%正常运行。我对PHP有一个初学者的理解 这是一个相对简单的应用程序,显示过去的考试文件,但对公众是锁定的。它在大多数情况下都正常工作,除了在样例应答文件中发生了一件奇怪的事情。并不是每个考试都有一个答案样本文件。没有的应该是空白的。但是,发生的情况是,一些没有相应答案文件的考试文件正在显示其他考试的考试文件按钮 你可以从这张图片中看到,1997年春季的第一次考试显示了一个空白区域,用于填写正确的样本答案。接下来的三
<div class="row">
<h4>Browse past exams</h4>
<form class="form-inline">
<div class="form-group">
<label class="col-sm-2">Course</label>
<div class="col-sm-4">
{{ Form::select('course', $courses, Input::get('course', ''), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2">Faculty</label>
<div class="col-sm-4">
{{ Form::select('faculty', $faculties, Input::get('faculty', ''), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
</form>
@if(PastExamsPublicController::isAdmin())
<div class="col-md-10 col-md-offset-1" style="margin-top: 25px; margin-bottom: 25px;">
<div class="btn-group" role="group">
<a href="{{ route('past-exams.utlaw.admin.exams.create') }}" class="btn btn-primary">
Add exam
</a>
<a href="{{ route('past-exams.utlaw.admin.faculty.index') }}" class="btn btn-primary">
Manage faculty
</a>
<a href="{{ route('past-exams.utlaw.admin.course.index') }}" class="btn btn-primary">
Manage courses
</a>
</div>
</div>
@endif
<hr>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1 col-xs-12">
<table class="table table-hover" id="past-exams-table" class="sort">
<thead>
<tr>
<th class='sort-default'>Course</th>
<th>Session/year</th>
<th>Faculty</th>
<th class='no-sort'>Exam</th>
<th class='no-sort'>Sample Answers</th>
</tr>
</thead>
<tbody>
@foreach($exams as $exam)
<tr>
<td>{{ $exam->course->name }}</td>
<td data-sort="{{ $exam->year . $exam->session->name }}" >
{{ $exam->session->name . '/' . $exam->year }}
</td>
<td>{{ $exam->faculty->last_name . ', ' . $exam->faculty->first_name }}</td>
<?php
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
?>
<td>
<a href="{{ asset('files/' . $examf) }}" class="btn btn-warning">
<span class="glyphicon glyphicon-download" aria-hidden="true"></span> Exam
</a>
</td>
<td>
@if(isset($answers))
<a href="{{ asset('files/' . $answers) }}" class="btn btn-primary">
<span class="glyphicon glyphicon-download" aria-hidden="true"></span> Sample Answers
</a>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
浏览过去的考试
课程
{{Form::select('course',$courses,Input::get('course',''),array('class'=>'Form control'))}
官能
{{Form::select('faculties',$faculties,Input::get('faculty',''),array('class'=>'Form control'))}
{{Form::submit('submit',array('class'=>'btn btn primary'))}
{{Form::close()}}
@如果(PasteXamPublicController::isAdmin())
@恩迪夫
课程
届会/年度
官能
考试
样本答案
@foreach($考试作为$考试)
{{$exam->课程->名称}
{{$exam->session->name.'/'.$exam->year}
{{$exam->faculty->last_name.','.$exam->faculty->first_name}
@如果(isset($answers))
@恩迪夫
@endforeach
提前感谢您的指导,帮助我指出错误的根源 您的问题存在于此:
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
这包含在一个foreach
循环中,因此每次检查都会运行。如果考试类型为“a”,则设置$answers
变量,但如果未设置,则不会清除该变量。建议在循环之前将其初始化为null
:
$answers = null;
$examf = null;
foreach($exam->files as $file) {
if($file->type == 'a') {
$answers = $file->filename;
}
else{
$examf = $file->filename;
}
}
编辑:正如@mopo922所指出的,您将希望对我在上面添加的
$examf
执行相同的操作。也可能对$examf
执行相同的操作。总的来说,我不喜欢foreach的foreach
——可能有更好的方法来实现这一目标。Jeremy和Mopo922,成功了!非常感谢!!我应该在图片中添加一个描述,但在这里输入图片描述是一个指向应用程序屏幕截图的链接。