看板 PHP 關於我們 聯絡資訊
Dears, 小弟目前在練習利用MVC模型建置網站, 但遇到了一點困難。 資料庫內有一張表單,共有14欄位,其中三欄為「code」、「name」、「office_date」 。 View 資料夾內的 List.tpl 檔如下: <table class="table table-hover"> <tr> <th>編號</th> <th>姓名</th> <th>日期</th> <th>紀錄</th> </tr> <!-- beginRow RowList --> <tr> <td>{RowList.code}</td> <td>{RowList.name}</td> <td>{RowList.office_date}</td> <td> <button type="button" class="btn btn-primary btn-xs" onClick="#">查看</button> </td> </tr> <!-- endRow RowList --> </table> Controller 資料夾內的 php 檔如下: public function doList() { // 檢查登入 & 權限 \Model\AccountModel::checkPermission(); $username = $_SESSION['CurrentAccount']['username']; $this->_tpl->setFile('list', 'List.tpl'); $mAttendant = new \Model\AttendantModel; $list = $mAttendant->getDataByAccount($username); $this->_tpl->setVar('username', $username); $this->_tpl->simpleBlock('list', 'RowList', $list, array($mAttendant, 'CallBackFunc_AttendantList')); return $this->_tpl->parse('list'); } Model 資料夾內的 php 檔如下: public function getDataByAccount($username) { $sql = 'SELECT code, name, office_date FROM '.\Core\Config::DATABASE_NAME.'.account WHERE username=:username'; $sth = $this->_pdoDB->prepare($sql); $sth->bindParam(':username', $username, \PDO::PARAM_STR); $sth->execute(); return $sth->fetch(\PDO::FETCH_ASSOC); } public function simpleBlock($parent, $block_name, $data, $callbackFunc = null) { if(!$data) { $this->clearBlockDefine($parent, $block_name); return; } $this->setBlock($parent, $block_name); $ctrlObj = $method = null; if (is_array($callbackFunc)) { $ctrlObj = $callbackFunc[0]; $method = $callbackFunc[1]; } for($i = 0, $cnt = count($data); $i < $cnt; $i++) { if (!empty($ctrlObj) && !empty($method)) $ctrlObj->$method($data[$i]); else if(function_exists($callbackFunc)) $callbackFunc($data[$i]); $this->setVar($block_name, $data[$i]); $this->parse($block_name, $i == $cnt - 1); } } public function CallBackFunc_AttendantList(&$arr) { } 但最後頁面卻呈現…… http://i.imgur.com/EGYLHku.png 看了一陣子還是不知道哪裡錯了,懇請各位指教說明! 先謝謝了! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.34.13 ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1488448318.A.449.html
Phedra: 雖然不清楚你用的是什麼 template…03/03 02:42
Phedra: 但我猜 simpleBlock() 方法裡面那一行 setVar() 沒寫好03/03 02:43
Phedra: 原本是寫 $this->setVar($data[$i])03/03 02:44
Phedra: 試試改成 $this->setVar($block_name, $data[$i]);03/03 02:44
Phedra: 參考依據是 $this->_tpl->setVar('username', $username);03/03 02:45
Phedra: 第一個參數值對應的是 template 裡面的 {username}03/03 02:46
Phedra: 第二個參數值則為要套用的資料03/03 02:46
好的,晚點來試試看!謝謝您!
latte0205: 能分享一下code嗎 能一起研究03/03 18:55
全部的code嗎?還是...? ============= 更新:已解決! ※ 編輯: starlit357 (115.82.179.226), 03/08/2017 18:59:07