繁体   English   中英

Laravel Web应用程序显示不正确的数据库条目

[英]Laravel web app displaying incorrect database entries

我正在尝试解决由前员工构建的Web应用程序无法正常运行100%的问题。 我对PHP有新手的了解。

这是一个相对简单的应用程序,可显示过去的考试文件,但不公开。 除了样本答案文件发生奇怪的事情以外,它在大多数情况下都可以正常工作。 并非每项考试都有一个示例答案文件。 没有一个的应该只是空白。 但是,发生的情况是某些没有相应答案文件的考试文件正在显示其他考试的考试文件按钮。

在此处输入图片说明

您可以从此图像中看到1997年春季的第一次考试显示了正确答案的空白。 接下来的三项考试也是正确的,因为它们显示了正确的答案文件,但是,后三项考试实际上并没有在数据库中包含示例答案文件,而是在显示2004年春季的示例答案,就像它们在重复最后一个文件一样。按特定顺序的名称。

我认为正在发生的事情是,如果没有相应的答案文件,则代码中没有明确说明要呈现空白的内容,但是我不知道是否确实如此。 这是来自index.blade.php的代码:

<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>

预先感谢您提供任何指导以帮助我指出错误的起因。

您的问题在这里:

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 ,这是我在上面添加的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM