Page 1 of 1

smarty and arrays(assoc): I can't get this right...

Posted: Thu Mar 27, 2008 9:21 am
by sarah_h
Hi all,

Using the debug in a smarty template I see this:

{$categories} Array (2)
                  0 => Array (3)
                                category_id => "4"
                                category_name => "cinema"
                                category_order => "50"
                  1 => Array (3)
                                category_id => "8"
                                category_name => "visiting theatre"
                                category_order => "50"


I need to be able to list each category like this:

cinema, visiting theatre

This is my code:

{foreach from=$categories.0 item='catkey' key ='catval'}
{if $catval=='category_name'}
{$catkey}
{/if}
{/foreach}

I've got this working but it's only look at element 0 and so I only see one category. I've tried nesting the foreach loops but I cannot get the syntax correct.

Would anyone be able to offer some guidance, I think I'm close!?

Sarah

Re: smarty and arrays(assoc): I can't get this right...

Posted: Thu Mar 27, 2008 9:49 am
by alby
sarah_h wrote: I've got this working but it's only look at element 0 and so I only see one category. I've tried nesting the foreach loops but I cannot get the syntax correct.

Would anyone be able to offer some guidance, I think I'm close!?
... from=$categories.0 is one array only (category_name => "cinema")
If you want display category names:

Code: Select all

{foreach from=$categories item='item' key ='key'}
{$item.category_name}<br />
{/foreach}
Alby

Re: smarty and arrays(assoc): I can't get this right...

Posted: Thu Mar 27, 2008 10:00 am
by sarah_h
I see! That did the trick. I'm learing this smarty stuff but I admit it does confuse me sometimes!

Thanks AGAIN for help Alby I appriciate it.

Sarah