代码之家  ›  专栏  ›  技术社区  ›  RageZ

Zend Framework如何获取循环中的项数?partialCounter和partialCounter?

  •  2
  • RageZ  · 技术社区  · 16 年前

    partialLoop 这个 documentation 提及 $this->partialCounter

    <?php
    if( $this->partialCounter == $mysteryvariable -1 ): 
    ?>
    

    3 回复  |  直到 16 年前
        1
  •  2
  •   TomáÅ¡ Fejfar    16 年前
    Zend_Registy::set('PartialCount', count($iterable));
    $this->partialLoop($script,$iterable);
    

    然后在你看来

    $count = Zend_Registy::get('PartialCount'); 
    

    又快又烂,但很有效。另一种方法是扩展PartialLoop助手;)

        2
  •  4
  •   Noah Goodrich    16 年前

    或者,我想说这可能更容易,在将对象传递到PartialLop之前,只需获取对象中的项目数,因为必须将可遍历对象或实际数组传递到PartialLop帮助器中,并且两者都实现对count()的支持。

    从文件中:

    <?php // partialLoop.phtml ?>
        <dt><?php echo $this->key ?></dt>
        <dd><?php echo $this->value ?></dd>
    
    
    <?php // MyController.php
    
        public function indexAction()
        {
            $this->view->$model = array(
                                     array('key' => 'Mammal', 'value' => 'Camel'),
                                     array('key' => 'Bird', 'value' => 'Penguin'),
                                     array('key' => 'Reptile', 'value' => 'Asp'),
                                     array('key' => 'Fish', 'value' => 'Flounder'),
                                  );
    
            $this->view->modelCount = count($this->view->model); 
        }
    

    从index.phmtl

    <p>Count: <?= $this->modelCount ?></p>
    <dl>
    <?php echo $this->partialLoop('partialLoop.phtml', $this->model) ?>
    </dl>
    
    推荐文章