|
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 |
wvdploeg Smarty Rookie
Joined: 05 Aug 2003 Posts: 19
|
Posted: Fri Sep 12, 2003 10:11 am Post subject: {section} and associative arrays |
|
|
Is it possible to walk to an associtive array with {section} and somehow retrieve the key name ? Because {section} has more possibilities I do not want to do it with {foreach}, but I did not find anywhere how I can show the actual keyvalue. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Sep 12, 2003 12:01 pm Post subject: |
|
|
no, section can only be used in conjunction with indexed arrays.
just curious: what are the "more possibilities" you have with sections?
greetings
messju |
|
Back to top |
|
wvdploeg Smarty Rookie
Joined: 05 Aug 2003 Posts: 19
|
Posted: Fri Sep 12, 2003 6:41 pm Post subject: |
|
|
messju wrote: | no, section can only be used in conjunction with indexed arrays.
just curious: what are the "more possibilities" you have with sections?
greetings
messju |
Well, according to the documentation it's also possible to use with associative arrays, they even have an example for it (although the content of the array is shown nowhere ), but when I want to present the data from an array in a way that I have two tables and element 1 has to be in table1, 2 in table 2, 3 again in one etc, I thought the start and the step property would be convenient. But I guess I have to split the resultset in 2 arrays then in the PHP code already. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Sep 12, 2003 6:49 pm Post subject: |
|
|
hiya wvdploeg.
I think that messju was only relating his response to your question: {section} doesn't handle the key portion of associative arrays. Certainly, you can iterate the contents of an associative array with {section} but it is really meant for indexed access.
I think it a little unnerving that PHP treats indexed and associative arrays as the same type, but what are you going to do? On the other hand it would be nice if there was only one catch-all loop structure in Smarty, but again... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sun Sep 14, 2003 1:15 pm Post subject: |
|
|
wvdploeg wrote: | Well, according to the documentation it's also possible to use with associative arrays |
yes, the loop-parameter only determines the loop-count. {section loop=$array...} is merely the same a {section loop=$array|@count} or {section loop=$integer} with $integer being sizeof($array). this also works with associative arrays but is pretty meaningless since you have no way to access the array's keys or values.
Quote: | but when I want to present the data from an array in a way that I have two tables and element 1 has to be in table1, 2 in table 2, 3 again in one etc, I thought the start and the step property would be convenient. But I guess I have to split the resultset in 2 arrays then in the PHP code already. |
this is of course a good and painless way to do it.
alternatives would be:
Code: |
{foreach name=loop from=$table....}{if ($smarty.foreach.loop.iteration % 2) ==1)}
first table
{/if}{/foreach}
{foreach name=loop from=$table....}{if ($smarty.foreach.loop.iteration % 2) ==0)}
second table
{/if}{/foreach}
|
or assign a lookup array:
$smarty->assign('table_lookup', array_keys($table));
and use this in the section
Code: |
{section loop=$table name=loop ...}{assign var=key value=$table_lookup[loop]}
... value: {$table.$key} ...
{/section}
|
greetings
messju |
|
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
|