|
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 |
desean84 Smarty Rookie
Joined: 04 Aug 2003 Posts: 7
|
Posted: Mon Aug 04, 2003 11:31 am Post subject: {section} to {foreach} |
|
|
hi, i'm still new to smarty so i hope to receive some help with the problem below:
how do i change the following {section} into {foreach}?
Code: | {section name=outer loop=$row}
<tr>
{section name=inner loop=$row[outer]}
<td>{$row[outer][inner]}</td>
{/section}
</tr>
{/section} |
$row is an assoc. array in the form of $row[number][alpha]. i've been trying hard to change but none of what i tried works.
also, i don't quite understand the usage of item attribute of foreach... can someone explain it to me more in-depth?
i also understand that the limitation of foreach is that it can only be used for a single array... what does that means?
thanks in advance for your reply as it means alot to me _________________ Dragons Online! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Aug 04, 2003 12:04 pm Post subject: Re: {section} to {foreach} |
|
|
desean84 wrote: |
also, i don't quite understand the usage of item attribute of foreach... can someone explain it to me more in-depth?
|
do you know php's foreach? ( http://php.net/manual/en/control-structures.foreach.php )
this php-foreach-loop:
[php:1:91e45f8f65]foreach ($array as $name=>$value) { echo $value; }[/php:1:91e45f8f65]
is comparable to this smarty-foreach-loop
Code: | {foreach from=$array key=name item=value} {$value} {/foreach} |
( "item=value" makes $value being updated on each iteration)
Quote: | i also understand that the limitation of foreach is that it can only be used for a single array... what does that means? |
me either. i think people saying this, mean, that you can do this with sections:
Code: |
{section name=foo from=$array}
var1: {$array[foo]}
var2: {$another_array[foo]}
{/section
|
(funky, not? ) but not with foreach.
but with foreach's "key"-attribute you get a comparable option:
Code: |
{foreach name=foo from=$array key=i item=not_used_but_required}
var1: {$array.$i}
var2: {$another_array.$i}
{/foreach}
|
to me {foreach} is far more intuitive than {section}, but maybe this is because i knew php's foreach far earlier than the existence of smarty |
|
Back to top |
|
desean84 Smarty Rookie
Joined: 04 Aug 2003 Posts: 7
|
Posted: Mon Aug 04, 2003 1:18 pm Post subject: |
|
|
thanks for your explanation messju!
i managed to solved my problem after reading it... thanks man!
in case anybody wants to know, it is:
Code: | {foreach from=$row key=number_row item=number}
<tr>
{foreach from=$number key=assoc_array item=value}
<td>{$value}</td>
{/foreach}
</tr>
{/foreach} |
_________________ Dragons Online! |
|
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
|