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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?