|
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 |
cens Smarty Rookie
Joined: 13 Nov 2003 Posts: 9
|
Posted: Thu Nov 13, 2003 9:34 pm Post subject: can't print both key and value of associative array |
|
|
I think that this is probably simple,. but I can't figure out how to output both the key and value of an array. Basically, I have an associative array that is dynamically generated, and I want to pass the array into smarty section and print the values and the keys.
My PHP CODE:
Code: |
foreach($_POST as $formitem => $value) {
$strata_val[$formitem] = $value;
}
$smarty->assign('strata_val',$strata_val);
|
And my Smarty Code:
Code: |
{section name=stratum loop=$strata_val}
<input type="hidden" name={$strata_val[stratum]} value={stratum} />
{/section}
|
Which should result in a bunch of hidden variables with the value being the key and the name being the value of the associative array $strata_val. Obvioulsy, that is not happening, and I cannot for the life of me figure out why.
Any Help is appreciated.
Thanks a lot.[/code] |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Nov 13, 2003 9:48 pm Post subject: |
|
|
use {foreach} instead of section, maybe. |
|
Back to top |
|
cens Smarty Rookie
Joined: 13 Nov 2003 Posts: 9
|
Posted: Thu Nov 13, 2003 9:57 pm Post subject: |
|
|
messju wrote: | use {foreach} instead of section, maybe. |
yeah, i just saw that in a different thread... asked too soon . Now I can get the value, but can't get the key to print. I think that I am copying this straight from the manual too (page34).
Code: |
{foreach name=stratum item=strat_val from=$strata_val}
{foreach key=key item=item from=$strat_val}
<input type="hidden" name={$key} value={$item} />
{/foreach}
{/foreach}
|
which results in:
Code: |
<input type="hidden" name=0 value=213131 />
<input type="hidden" name=0 value=534452 />
<input type="hidden" name=0 value=24566634 />
<input type="hidden" name=0 value=524242 />
<input type="hidden" name=0 value=5534567645 />
<input type="hidden" name=0 value=4242423 />
|
so the name field is obviously wrong.
Ideas? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Nov 13, 2003 10:04 pm Post subject: |
|
|
maybe read the manual a little closer and use the "key"-attribute of {foreach}. |
|
Back to top |
|
cens Smarty Rookie
Joined: 13 Nov 2003 Posts: 9
|
Posted: Thu Nov 13, 2003 10:11 pm Post subject: |
|
|
Okay-
Fully embarassed now. Thanks for the help. |
|
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
|