doujiao3072 2011-12-05 09:47
浏览 215
已采纳

在Zend的index.phtml中为foreach()提供的参数无效

The code for index.phtml as follows:

<?php foreach ($this->entries as $entry): ?>
    <?php echo $this->escape($entry->email) ?>
    <br></br>
    <?php echo $this->escape($entry->comment) ?>
<?php endforeach ?>

The code for indexAction() in IndexController:

public function indexAction()
{

    $guestbook = new Application_Model_HRModel();
    $view = new Zend_View(array('scriptPath' =>'C:/Users/398853/Documents/NetBeansProjects/PhpProject3/application/views/scripts/index'));
    $view->entries = $guestbook->fetchAll();
    echo $view->render('index.phtml');

}

The code for fetchAll() in Application_Model_HRModel:

    public function fetchAll() 
   {
       $entry = new Application_Model_HRMo();
       $resultSet = $this->getDbTable()->fetchAll();
       $entries   = array();
       foreach ($resultSet as $row) {
        $entry->setId($row->id);
        $entry->setEmail($row->email);       
        $entry->setCreated($row->created);
        $entry->setComment($row->comment);
        $entries[] = $entry;
    }

            return $entries;

} I have 3 entries i.e. 3 rows in my databse table . But When i request the url as http://localhost:8888/Index it says Warning: Invalid argument supplied for foreach() in C:\Users\39885Documents\NetBeansProjects\PhpProject3\application\views\scripts\index\index.phtml on line 54 and then display last row's entry 3 times.I think problem occurs because at first it executes index.phtml and without executing indexAction()(which further executes fetchAll()) $entries will not an array Thats Why at first it gives above stated warning. Now tell me how to start execution from indexAction() then come at index.phtml so that $entries would be an array.

  • 写回答

1条回答 默认 最新

  • dongyuhui0418 2011-12-05 10:07
    关注

    In fetchAll() you use the same object, but you should create new in loop. That's why you have 3 duplicated rows in output. About PHP warning - tru to var_dump($this->entries) and check the type

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集