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