|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
opojal Smarty n00b
Joined: 14 Dec 2007 Posts: 1
|
Posted: Fri Dec 14, 2007 3:29 pm Post subject: changing array in foreach |
|
|
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...
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 |
|
conner_bw Smarty Rookie
Joined: 21 Dec 2007 Posts: 17
|
Posted: Fri Dec 21, 2007 7:40 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Dec 22, 2007 12:20 am Post subject: |
|
|
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 |
|
|
|
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
|