dsyua2828 2012-04-27 09: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 11: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 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部