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

can't print both key and value of associative array

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


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 13, 2003 9:34 pm    Post subject: can't print both key and value of associative array Reply with quote

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


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

PostPosted: Thu Nov 13, 2003 9:48 pm    Post subject: Reply with quote

use {foreach} instead of section, maybe.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cens
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 13, 2003 9:57 pm    Post subject: Reply with quote

messju wrote:
use {foreach} instead of section, maybe.


yeah, i just saw that in a different thread... asked too soon Embarassed . 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
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Nov 13, 2003 10:04 pm    Post subject: Reply with quote

maybe read the manual a little closer and use the "key"-attribute of {foreach}.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cens
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 13, 2003 10:11 pm    Post subject: Reply with quote

Okay-
Fully embarassed now.Embarassed Thanks for the help.
Back to top
View user's profile Send private message
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