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

Nesting 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
irbrian
Smarty Rookie


Joined: 08 Oct 2003
Posts: 32
Location: USA

PostPosted: Wed Jun 02, 2004 11:26 pm    Post subject: Nesting Associative Arrays? Reply with quote

Is there any way to nest associative arrays, i.e.:
Code:

<body>
    {$TEXT.$MSGCODE.user}
</body>

That's a crude example, but I get an error whenever I try to use something like that.

However, I'm finding it necessary because the product I'm building passes a language array to Smarty in order to allow multiple language support, and also passes generated arrays of code values to Smarty to handle various situations; and I'd like to use them together in this way to generate the output.

If its not possible, does anyone have a good workaround?
_________________
I Create. Therefore I Am.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Wed Jun 02, 2004 11:42 pm    Post subject: Reply with quote

The problem here is that it is indeterminate: does that mean $text[$MSGCODE.user] or $text[$MSGCODE].user ?

The usual workaround is a temporary var:

{assign var=temp value=$MSGCODE.user}
{$text.$temp}

but these work (depending on what you are after):

{$text[$MSGCODE.user]}
{$text[$MSGCODE].user}
Back to top
View user's profile Send private message
irbrian
Smarty Rookie


Joined: 08 Oct 2003
Posts: 32
Location: USA

PostPosted: Thu Jun 03, 2004 5:35 pm    Post subject: Reply with quote

You're right on, boots. I needed to use the following syntax:

{$TEXT[$MSGCODE.user]}

Thanks for the help!
_________________
I Create. Therefore I Am.
Back to top
View user's profile Send private message Visit poster's website
irbrian
Smarty Rookie


Joined: 08 Oct 2003
Posts: 32
Location: USA

PostPosted: Sat Jun 12, 2004 12:23 am    Post subject: Reply with quote

I apologize for resurrecting this topic, but I've encountered roughly the same situation again, and the previous solution isn't working. This time I've got an array nested 4 layers deep.

Obviously it might seem more practical to just split out my arrays, but in this case I'm using a lot of foreach loops and passing arrays in and out of temp data storage, and its just easier to use the nesting.

Here's the structure :

Code:
Contents of $PEOPLE:
Array
(
    [some_var] => 1,
    [user_id] => 13,
    [user_first_name] => John,
    [user_last_name] => Doe,
    [friend] => Array
        (
            [4] => Array
                (
                    [first_name] => Bob,
                    [last_name] => Jones
                )
            [7] => Array
                (
                    [first_name] => Don,
                    [last_name] => Johnson
                )
        )
)

My question is, how I would reference the first and last name fields within the friends array, as in the following template snippet (note that I'm just using brackets here in place of whatever the correct usage would be, in order to show appropriate nesting):

{$PEOPLE[friend][$my_friend][first_name]}
_________________
I Create. Therefore I Am.
Back to top
View user's profile Send private message Visit poster's website
jaey
Smarty Regular


Joined: 25 May 2004
Posts: 36
Location: Louisville, Kentucky, USA

PostPosted: Sat Jun 12, 2004 1:13 am    Post subject: Reply with quote

Try this:

{$PEOPLE.friend[$my_friend].first_name}
Back to top
View user's profile Send private message
alexh
Smarty Rookie


Joined: 19 Feb 2004
Posts: 10
Location: London

PostPosted: Mon Jun 21, 2004 3:07 pm    Post subject: Reply with quote

Like the previous guy sorry to resurrect this thread but i have roughly the same problem which i can't seem to find a solution to. To be frank I am new to both Smarty and multi-dimensional arrays so i feel like i am tying myself in knots.

I have an array 3 layers deep, that i am trying to display using [section]. The top layer($month) holds months 1-8. The second layer holds all the results for each month. The third layer holds the individual records for each result.


Can anyone explain how i access the records in the third layer and display them using [section] or maybe a work around? Thanks.


PHP:
Code:
$film = array();
$month = array();

for ($i = 0; $i <= 8; $i++){
   $query ="SELECT filmID, title, date from film where month(date) = month(curdate()) + $i order by date";
   
   $result = $db ->dbQuery($query);

   //assign values to arrays
   while ($row = mysql_fetch_array($result)){
      $month[$i] = $film[array(
'filmID' =>$row['filmID'],
'title' => $row['title'],
'date' => $row['date']
)];
   }

}
//assign to smarty
$smarty->assign('month', $month);


Template:

Code:

{section name=schedule loop=$month}
<tr>
<td class="topten_bold">{$month[$film.filmID][schedule]}</td>
<td class="topten">{$month[$film.title][schedule]}</td>
<td class="topten_bold">{$month[$film.date][schedule]}</td>
</tr>
{/section}
[/code]
Back to top
View user's profile Send private message MSN Messenger
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