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

{section} and associative arrays

 
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
wvdploeg
Smarty Rookie


Joined: 05 Aug 2003
Posts: 19

PostPosted: Fri Sep 12, 2003 10:11 am    Post subject: {section} and associative arrays Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Sep 12, 2003 12:01 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
wvdploeg
Smarty Rookie


Joined: 05 Aug 2003
Posts: 19

PostPosted: Fri Sep 12, 2003 6:41 pm    Post subject: Reply with quote

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 Smile), 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
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri Sep 12, 2003 6:49 pm    Post subject: Reply with quote

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... Smile
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Sep 14, 2003 1:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
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