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


Zend fetchAll foreach 2 times run dont loop 2nd time;

这很容易理解

图像类

<?php
class Image extends Zend_Db_Table_Abstract {
    protected $_name = 'images';
    public function getList() {
        return $this->fetchAll();
    }
}?>

我的 PHP 代码

<?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
?>

我可以使用$this访问模板内的模板变量

下面的代码来自模板内部

$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
}
?>

上面的代码工作正常,但在几行下面,我必须再次迭代相同的数据,不要输出任何东西。如果我评论第一个,第二个开始工作。

第一个是创建JS数组,在头部部分,第二部分是HTML显示图像

我希望这是一个指针问题,我可能必须将循环当前项目设置为启动,但我现在不明白.... reset($this->imgList)不起作用

请帮忙

我认为这与fetchAll调用有关,请尝试以下操作:

<?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;
    }
}?>