作者starlit357 (OLAF)
看板PHP
标题[请益] MVC模型 Array值传接问题
时间Thu Mar 2 17:51:52 2017
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://webptt.com/cn.aspx?n=bbs/PHP/M.1488448318.A.449.html
1F:→ Phedra: 虽然不清楚你用的是什麽 template…03/03 02:42
2F:→ Phedra: 但我猜 simpleBlock() 方法里面那一行 setVar() 没写好03/03 02:43
3F:→ Phedra: 原本是写 $this->setVar($data[$i])03/03 02:44
4F:→ Phedra: 试试改成 $this->setVar($block_name, $data[$i]);03/03 02:44
5F:→ Phedra: 参考依据是 $this->_tpl->setVar('username', $username);03/03 02:45
6F:→ Phedra: 第一个参数值对应的是 template 里面的 {username}03/03 02:46
7F:→ Phedra: 第二个参数值则为要套用的资料03/03 02:46
好的,晚点来试试看!谢谢您!
8F:→ latte0205: 能分享一下code吗 能一起研究03/03 18:55
全部的code吗?还是...?
=============
更新:已解决!
※ 编辑: starlit357 (115.82.179.226), 03/08/2017 18:59:07