[solved]foreach

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
klompie
New Member
New Member
Posts: 3
Joined: Sat Sep 12, 2009 6:08 pm

[solved]foreach

Post 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}
Last edited by klompie on Wed Oct 14, 2009 1:19 pm, edited 1 time in total.
Peciura

Re: [solved]foreach

Post 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
klompie
New Member
New Member
Posts: 3
Joined: Sat Sep 12, 2009 6:08 pm

Re: [solved]foreach

Post by klompie »

thanx this solution is better ;D
Post Reply

Return to “CMSMS Core”