我有一個課程描述網頁作為一個大學項目,我目前正在該頁面上顯示有關所有課程批準狀態的信息。我正在將存儲在數據庫中的所有課程檢索到 php 中的數組,然后將此數組傳遞到我想要遍歷課程數組的 twig 文件。但是 twig 似乎無法識別“courseList”中的“course”變量是什么,因為沒有顯示任何信息。枝條<tbody> {% for course in courseList %} <tr> <th scope="row">{{ loop.index }}</th> <td>{{ course.getCourseCode }}</td> <td>{{ course.getCourseName(0, course.getCourseId) }}</td> <td class="font-weight-bold">{{ course.getCourseApprovalDue }}</td> <td> <div class="progress"> <div class="progress-bar {% if course.getStatus == 0 %} bg-warning {% elseif coruse.getStatus == 1 %} bg-danger {% else %} bg-success {% endif %}" role="progressbar" style="width: 100%" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10">{{ course.getStatus }} </div> </div> </td> </tr> {% endfor %} </tbody>PHPif ($session->has('loggedin')) { $courseList = $database->getAllCourses(); echo $twig->render('courseStatus.twig', array('user' => $session->get('user'), 'loggedIn' => true, 'courseList' => $courseList));} else { echo $twig->render('login.twig');}數據庫查詢和 php 函數工作正常。我可以通過將硬編碼的課程對象傳遞給樹枝文件來驗證這一點。coruseList 數組也不為空,因為 twig 文件在渲染時顯示 5 行,這與存儲在數據庫中的課程數量相同。
1 回答

開滿天機
TA貢獻1786條經驗 獲得超13個贊
從 getAllCourses() 返回的數組不會創建 Course 的新實例,它只是一個包含帶有 Course-data 的數組的數組。解決方案是獲取數組,并使用返回的信息創建一個包含 Course-objects 的新數組,并將該數組傳遞給 php-file。
- 1 回答
- 0 關注
- 94 瀏覽
添加回答
舉報
0/150
提交
取消