我的控制器功能是
public function listAction()
{
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'',
'dbname' =>'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
$sql = "SELECT * FROM `user` ORDER BY user_name ASC";
$result = $DB->fetchAssoc($sql);
echo count($result);
$this->view->assign('title','Member List');
$this->view->assign('description','Below, our members:');
$this->view->assign('datas',$result);
}
和phtml文件是通过
获取mysql表<?php
$datas = $this->datas;
for($i = 1; $i<= count($datas);$i++)
{ echo $datas[$i]['id'];
echo $datas[$i]['user_name'];
echo $datas[$i]['first_name'];
echo $datas[$i]['last_name'];
} ?>
它显示未定义偏移量= 1的错误,如果我从2开始循环它就可以了,并且显示一个较少的行形式mysql表。
请帮助,我没有得到错误的地方
答案 0 :(得分:1)
因为$ datas的数组索引不是1.用以下代码替换你的phtml文件。希望有所帮助。
<?php
$datas = $this->datas;
foreach ($datas as $data) {
echo $data['id'];
echo $data['user_name'];
echo $data['first_name'];
echo $data['last_name'];
}
?>