Zend Framework how to get the number of item in the loop ? partialLoop partialCounter and?
I want to to make a particular processing for the last item of a partialLoop
, the documentation mention about $this->partialCounter
but not the variable with the total number of items ...
<?php
if( $this->partialCounter == $mysteryvariable -1 ):
?>
I am missing something I 开发者_如何学JAVAthink ... cannot get my hand on that variable ...
In order to get the total number of items, you will have to either extend Zend_View_Helper_PartialLoop to provide a method that returns the count of the iterable object being used by the PartialLoop.
Or, and I would say this is probably easier, just get the count of items in the object before you pass it into the PartialLoop since you have to pass either a Traversable object or an actual array into the PartialLoop helper and both implement support for count().
From the documentation:
<?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);
}
From index.phmtl
<p>Count: <?= $this->modelCount ?></p>
<dl>
<?php echo $this->partialLoop('partialLoop.phtml', $this->model) ?>
</dl>
Zend_Registy::set('PartialCount', count($iterable));
$this->partialLoop($script,$iterable);
and then in your view
$count = Zend_Registy::get('PartialCount');
Fast and crappy, but working. Other way would be to to extend PartialLoop helper ;)
I knew this is an old issue, but in ZF 1.12.1 this will be fixed. Till then there is a patch to get under following URL:
http://framework.zend.com/issues/browse/ZF-7151
Regards, Sascha
精彩评论