[請益] MVC模型 Array值傳接問題 - 已解決

看板PHP作者 (OLAF)時間7年前 (2017/03/02 17:51), 7年前編輯推噓0(008)
留言8則, 2人參與, 最新討論串1/1
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

03/03 02:42, , 1F
雖然不清楚你用的是什麼 template…
03/03 02:42, 1F

03/03 02:43, , 2F
但我猜 simpleBlock() 方法裡面那一行 setVar() 沒寫好
03/03 02:43, 2F

03/03 02:44, , 3F
原本是寫 $this->setVar($data[$i])
03/03 02:44, 3F

03/03 02:44, , 4F
試試改成 $this->setVar($block_name, $data[$i]);
03/03 02:44, 4F

03/03 02:45, , 5F
參考依據是 $this->_tpl->setVar('username', $username);
03/03 02:45, 5F

03/03 02:46, , 6F
第一個參數值對應的是 template 裡面的 {username}
03/03 02:46, 6F

03/03 02:46, , 7F
第二個參數值則為要套用的資料
03/03 02:46, 7F
好的,晚點來試試看!謝謝您!

03/03 18:55, , 8F
能分享一下code嗎 能一起研究
03/03 18:55, 8F
全部的code嗎?還是...? ============= 更新:已解決! ※ 編輯: starlit357 (115.82.179.226), 03/08/2017 18:59:07
文章代碼(AID): #1Oj-i-H9 (PHP)