简体   繁体   中英

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. I have a novice understanding of 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. 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.

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:

<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. If the exam type is "a", the $answers variable gets set, but there is nothing that clears it if it is not set. Would recommend initializing it to null before the loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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