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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
sarah_h
Forum Members
Forum Members
Posts: 189
Joined: Tue Apr 25, 2006 8:30 am

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

Post 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
alby

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

Post 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
sarah_h
Forum Members
Forum Members
Posts: 189
Joined: Tue Apr 25, 2006 8:30 am

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

Post 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
Post Reply

Return to “CMSMS Core”