Somacon.com: Articles on web development, software, and hardware
§ Home > Index > Web Development

Alternating Row Colors with Zend PartialLoop

How to alternate table or list row colors in the partialLoop call of Zend.

The code snippets below should give you the idea. Every alternating TR tag will have a class of "alt". Then, in the style sheet, just define a background color for TR tags with that class. The trick here is to save the state in the parent view, which is accessed through the PartialLoop helper. You can also use this trick to print index numbers or sequences in the list.

partialList.phtml


<tr class="<?php print (!$this->getHelper('PartialLoop')->view->altrow ^= 1) ? 'alt' '';    ?>">
    <td><?php echo $this->escape($this->Id); ?></td>
    <td><?php echo $this->escape($this->FirstName); ?></td>
    <td><?php echo $this->escape($this->LastName); ?></td>
</tr>

style.css


<style type="text/css">
table.example tr.alt td { background-color: gray; }
</style>

index.phtml


<table class="example">
<tr>
    <th>Id</th>
    <th>Last</th>
    <th>First</th>
</tr>
<?php echo $this->partialLoop('example/partialList.phtml'$this->names); ?>
</table>
Link to this page: <a href="http://www.somacon.com/p540.php">Alternating Row Colors with Zend PartialLoop</a>

Contact · Search · Print · Social bookmark this page · E-mail this page · Web Developer For Hire
Created 2008-06-26, Last Modified 2008-06-26, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.