Page 1 of 1

[solved]foreach

Posted: Wed Oct 14, 2009 12:42 pm
by klompie
i use {foreach ...} to generate a list of items but is it possible to have a number counting with the loop?
So it the first item i can echo a 0 in the second a 1 the third a 2 and so on...
i want to have that number on the place of the ???

{foreach from=$albums item=cur_album}
var Slides{$cur_album->id}=[]
{foreach from=$cur_album->PicCont item=onepicture}
Slides{$cur_album->id}[  ???  ] = ["{$onepicture->picture}", "{$onepicture->comment}"];
{/foreach}
{/foreach}

EDIT:
found it.
using this:

{foreach from=$albums item=cur_album}
{assign var=tempid value=0}
var Slides{$cur_album->id}=[]
{foreach from=$cur_album->PicCont item=onepicture}
Slides{$cur_album->id}[{$tempid}] = ["{$onepicture->picture}", "{$onepicture->comment|escape:'html'}"];
{assign var=tempid value=$tempid+1}
{/foreach}
{/foreach}

Re: [solved]foreach

Posted: Wed Oct 14, 2009 1:22 pm
by Peciura
Try to use key parameter in "{foreach}"tag

Code: Select all

{foreach key=key item=item from=$contact}
    {$key}: {$item}<br />
{/foreach}
http://www.smarty.net/manual/en/language.function.foreach.php

For more advanced loops check "{section}" tag at
http://www.smarty.net/manual/en/language.function.section.php

Re: [solved]foreach

Posted: Wed Oct 14, 2009 1:55 pm
by klompie
thanx this solution is better ;D