Re: [SMARTY] using index_prev with foreach
| From: | messju mohr | Date: | Wed, 04 Aug 2004 11:48:08 +0000 |
| Subject: | Re: [SMARTY] using index_prev with foreach | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11659@lists.php.net to get a copy of this message | ||
On Wed, Aug 04, 2004 at 10:43:01AM +0200, Hiro Protagonist wrote:
> Hi,
>
> I have an array, with e.g. this keys: unit, name, phone.
> I would like to loop through the array with foreach (works fine) and
> would like to put out a extra <tr>, if the unit changes. So the output
> will be grouped by unit.
>
> I tried this:
>
> {foreach name=members from=$arrayMembers item=mem}
> {if $smarty.foreach.members.first or
> $smarty.foreach.members.index_prev.unit ne $smarty.foreach.members.unit}
> Special unit-Row
> {/if}
> Normal-Row
> {/foreach}
>
> Doesn't work.
>
> It works with sections, but i thought, doing it with foreach would make
> the code simpler. Any ideas?
you have to handle this yourself and emulate an index_prev:
{foreach name=members from=$arrayMembers item=mem}
{if $smarty.foreach.members.first or $recent_unit ne $mem.unit}
Special unit-Row
{/if}
Normal-Row
{assign var=recent_unit value=$mem.unit}
{/foreach}
maybe it's not much simpler than with section, just a little
different. it may be a matter of taste if one prefers foreach or
section. i go for foreach - almost always.
> thx
>
> Hiro