tags:

views:

87

answers:

1

I am having trouble getting the below array to display properly in a smarty template file. If anyone can help point me in the right direction I would be most thankful.

my template code looks like this:

          {foreach from=$trackingGroups item=gender key=k item=v}
          {assign var=tc value="`$v.id`"}    
          {$v.title} ({$v.productCount})<br />

{foreach item=department from=$trackingGroups.$tc item=v2} {$v2.title}
{/foreach} {/foreach}

but the page displays like this:

          Women (3)<br />
           W<br />
                    7<br />
                    3<br />
                    Footwear<br />
                    Accessories<br />

          Men (2)<br />
           M<br />
                    7<br />
                    2<br />
                    Footwear<br />




Array

( [71] => Array ( [title] => Women [id] => 71 [productCount] => 3 [171] => Array ( [id] => 171 [title] => Footwear [productCount] => 2 [74] => Array ( [id] => 74 [title] => Boots [productCount] => 2 )

            )

        [172] => Array
            (
                [id] => 172
                [title] => Accessories
                [productCount] => 1
                [74] => Array
                    (
                        [id] => 74
                        [title] => Boots
                        [productCount] => 1
                    )

            )

    )

[72] => Array
    (
        [title] => Men
        [id] => 72
        [productCount] => 2
        [171] => Array
            (
                [id] => 171
                [title] => Footwear
                [productCount] => 2
                [74] => Array
                    (
                        [id] => 74
                        [title] => Boots
                        [productCount] => 2
                    )

            )

    )

)

A: 

When you are looping in your second foreach you are looping through all the elements, not just the title keys. For example:

{foreach item=department from=$trackingGroups.72 item=v2} 
   {$v2.title}
{/foreach}

in this case $v2 will take the following values:

[title] => Men
[id] => 72 
[productCount] => 2 
[171] => Array 
 ( 
                [id] => 171 
                [title] => Footwear 
                [productCount] => 2 
                [74] => Array 
                    ( 
                        [id] => 74 
                        [title] => Boots 
                        [productCount] => 2 
                    ) 

 ) 

The first, second and third value will be only a string. Since it is not an array, $v2.title will return the "title"'th element (the first one), returning the first character: M,7,2 The fourth element is an array with the "title" key, and it is returned correctly: Footwear.

Zsolti