dsyua2828 2012-04-27 17:43
浏览 18
已采纳

Zend fetchAll foreach 2次运行不第二次循环;

This is very simple to understand

Image Class

<?php
class Image extends Zend_Db_Table_Abstract {

    protected $_name = 'images';

    public function getList() {
        return $this->fetchAll();
    }
}?>

My PHP Code

<?php

require 'config.php';

$imgTable = new Image();  // Create Object

$imgList = $imgTable->getList(); // fetch Data

$template = new Template('portfolio'); // Initialize Template and tell which template to pick

$template->imgList = $imgList; // set template variable

$template->render(); // Generate Template output

?>

I can access template variable inside template using $this

Below code is from inside the template

$xback = 0;
foreach ($this->imgList as $images) {
    echo 'imageArray[' . $xback . '] = "' . $images['sef'] . '";';
    $xback++;
}
?>
.......
<?php


foreach ($this->imgList as $images) {

?>
    <div class="portfolio_item">
    <img src="<?php echo PATH_WEB . $images['image_thumb'] ?>" height="146" width="209" />
    <div class="title"><?php echo $images['image_title'] ?></div>
    <div class="right link">
        <a href="javascript:;" onclick="showItemDetails('<?php echo $images['sef'] ?>')">View Details</a>
    </div>
    </div>
<?php
}

?>

Above code is working fine, but below some few lines, I have to iterate over the same data again dont output any thing. If I comment the first one, 2nd starts working.

First one is to create the JS array and is in head section, Second part is in HTML to display images

I hope its a pointer issue, I may have to set the loop current item to start, but I am not understanding it right now .... reset($this->imgList) didnt worked

please help

  • 写回答

1条回答 默认 最新

  • douhui1957 2012-04-27 19:46
    关注

    I think it has something to do with the fetchAll call, try this:

    <?php
    class Image extends Zend_Db_Table_Abstract {
    
        protected $_name = 'images';
        protected $images;
    
        public function getList() {
    
            // Employ lazy loading pattern to load images if they aren't set yet
            if (!isset($this->$images)) {
                $this->images = $this->fetchAll();
            }
    
            return $this->images;
        }
    }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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#的问题,如何解决?