Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

changing array in foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
opojal
Smarty n00b


Joined: 14 Dec 2007
Posts: 1

PostPosted: Fri Dec 14, 2007 3:29 pm    Post subject: changing array in foreach Reply with quote

hi, i am a new smarty user...and wondering how to 'automatically' change the array that is going to looped in a foreach??

Code:
forexample:
{foreach from=$from1 item="item"}
    do something...
{/foreach}

what i want is the number 1 in $from1 is changed based on the input...
let say if i press 2, than it should be $from2...

i tried to use {foreach from=from$num ...} or something like that..
and it didnt work... Embarassed

anybody can help...really appreciate it...

PS: hope you guys can understand my question....
had a hard time to formulate the question...hehehe...
Back to top
View user's profile Send private message
conner_bw
Smarty Rookie


Joined: 21 Dec 2007
Posts: 17

PostPosted: Fri Dec 21, 2007 7:40 pm    Post subject: Reply with quote

Maybe try:

Code:

{if $from1}
    {assign var='from' value=$from1}
{elseif $from2}
    {assign var='from' value=$from2}
{elseif $from3}
    {assign var='from' value=$from3}
{/if}

{foreach from=$from item="item"}
    do something...
{/foreach}
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Dec 22, 2007 12:20 am    Post subject: Reply with quote

Perhaps you can assign all of the individual arrays you want to process into an array. That way you can index into the array to get the desired array of choice to loop over. eg:
Code:
$smarty->assign('arrays', compact( 'from1', 'from2', 'from3') );
$smarty->assign('selected', 'from2' );
---
{foreach from=$arrays.$selected item=item}
...

Generally, anytime you think you need a var-var (which Smarty does not support) consider instead a 2D array.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP