我在indexAction中进行一个数据库查找,将返回的记录保存在$view->post中,然后到页面上述出来,数据库查找没有问题,我这里indexAction中尝试把$row循环数出来时一切正常的
class IndexController extends Zend_Controller_Action
{
function indexAction()
{
Zend_Loader::loadClass('Zend_View');
$view=new Zend_view();
$view->setScriptPath('./application/views/scripts');
$view->title='hello world, zend framework. where is index';
/////
require_once('./application/models/post.php');
$post=new tbposts();
$db=$post->getAdapter();
//+$where=$db->quoteInfo('userid=?',1);
$row=$post->fetchAll();
$view->post=$row;
/*我在这里输出是正常的
foreach($row as $onepost)
{
echo ($onepost->words.'<br>');
}
*/
/////
echo $view->render("index/index.phtml");
}
}
问题是我在phtml中循环输出记录是遇到一个warning,虽然我需要的内容也被正确的输出了
<?php
echo ($this->title);
?>
<?php
foreach($this->post as $post)
{
echo ($post->words.'<br>');
}
/*
while($post=each($alldata))
{
echo $post->words.'<br>';
}
*/
?>
遇到一个warnning:
Warning: Invalid argument supplied for foreach() in E:\apachewwwroot\zf\html\application\views\scripts\index\index.phtml on line 16
我搜索了之后,看到大部分都是说foreach中不是数组或空,首先$this->post肯定不为空,(因为$post->words的输出内容是正确的),然而我用is_array($this->post)返回的的确是false.
那为什么foreach中还是执行了循环并输出了$this->post呢,为什么有这个warning呢?
class IndexController extends Zend_Controller_Action
{
function indexAction()
{
Zend_Loader::loadClass('Zend_View');
$view=new Zend_view();
$view->setScriptPath('./application/views/scripts');
$view->title='hello world, zend framework. where is index';
/////
require_once('./application/models/post.php');
$post=new tbposts();
$db=$post->getAdapter();
//+$where=$db->quoteInfo('userid=?',1);
$row=$post->fetchAll();
$view->post=$row;
/*我在这里输出是正常的
foreach($row as $onepost)
{
echo ($onepost->words.'<br>');
}
*/
/////
echo $view->render("index/index.phtml");
}
}
问题是我在phtml中循环输出记录是遇到一个warning,虽然我需要的内容也被正确的输出了
<?php
echo ($this->title);
?>
<?php
foreach($this->post as $post)
{
echo ($post->words.'<br>');
}
/*
while($post=each($alldata))
{
echo $post->words.'<br>';
}
*/
?>
遇到一个warnning:
Warning: Invalid argument supplied for foreach() in E:\apachewwwroot\zf\html\application\views\scripts\index\index.phtml on line 16
我搜索了之后,看到大部分都是说foreach中不是数组或空,首先$this->post肯定不为空,(因为$post->words的输出内容是正确的),然而我用is_array($this->post)返回的的确是false.
那为什么foreach中还是执行了循环并输出了$this->post呢,为什么有这个warning呢?
我还用了
echo var_dump($alldata);
来看他的内容,可是我不知道我应该怎么改啊,我用array强制换类型也没用啊
if($this->post)
{
foreach(......)
}
就好了,不知道为什么会这样。