简体   繁体   English

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

[英]Laravel web app displaying incorrect database entries

I am trying to troubleshoot a web app that is not functioning 100% correctly that was built by a former employee. 我正在尝试解决由前员工构建的Web应用程序无法正常运行100%的问题。 I have a novice understanding of PHP. 我对PHP有新手的了解。

This is a relatively simple app that displays past exam files, but is locked down to the public. 这是一个相对简单的应用程序,可显示过去的考试文件,但不公开。 It is working correctly for the most part except for a weird thing happening with sample answer files. 除了样本答案文件发生奇怪的事情以外,它在大多数情况下都可以正常工作。 Not every exam has a sample answer file. 并非每项考试都有一个示例答案文件。 The ones that don't have one should just be blank. 没有一个的应该只是空白。 However what is happening is that some exam files that do not have a corresponding answer file are displaying the button for exam files for other exams. 但是,发生的情况是某些没有相应答案文件的考试文件正在显示其他考试的考试文件按钮。

在此处输入图片说明

You can see from this image, that the first exam from Spring 1997 shows a blank space for sample answer which is correct. 您可以从此图像中看到1997年春季的第一次考试显示了正确答案的空白。 the next three exams are also correct in that they are showing the correct answer file, however, the last three exams do not actually have sample answer files in the database but are showing the sample answer for Spring 2004, like they are repeating the last file name that is in a specific sequence. 接下来的三项考试也是正确的,因为它们显示了正确的答案文件,但是,后三项考试实际上并没有在数据库中包含示例答案文件,而是在显示2004年春季的示例答案,就像它们在重复最后一个文件一样。按特定顺序的名称。

What I think is happening is that there is nothing in the code specifically saying to render a blank space if there is no corresponding answer file, but I don't know if this is actually the case. 我认为正在发生的事情是,如果没有相应的答案文件,则代码中没有明确说明要呈现空白的内容,但是我不知道是否确实如此。 This is the code from index.blade.php: 这是来自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>

Thank you in advance for any guidance in helping point me to the genesis of the error. 预先感谢您提供任何指导以帮助我指出错误的起因。

Your problem exists here: 您的问题在这里:

foreach($exam->files as $file) {
    if($file->type == 'a') {
        $answers = $file->filename;
    }
    else{
        $examf = $file->filename;
    }
}

This is contained inside a foreach loop so gets run for each exam. 它包含在一个foreach循环中,因此可以在每次考试时运行。 If the exam type is "a", the $answers variable gets set, but there is nothing that clears it if it is not set. 如果检查类型为“ a”,则设置$answers变量,但是如果未设置,则没有任何内容可以清除它。 Would recommend initializing it to null before the loop: 建议在循环之前将其初始化为null

$answers = null;
$examf = null;
foreach($exam->files as $file) {
    if($file->type == 'a') {
        $answers = $file->filename;
    }
    else{
        $examf = $file->filename;
    }
}

EDIT: As pointed out by @mopo922, you will want to do the same with $examf , which I added above. 编辑:正如@ mopo922指出的那样,您将要对$examf进行相同的$examf ,这是我在上面添加的。

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

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