Page 1 of 1
Associative arrays with smarty
Posted: Thu Feb 01, 2007 4:41 pm
by GeXus
If anyone is a smarty pro, I have a question I cannot seem to find an answer for... I've built the following array, which will be used to display a series of categories and sub-categories (ie, directory). I'm trying to display it in 3 columns (ie, typical directory style) but I'm not having any luck. Any help would be amazing! Thanks
Array:
Code: Select all
Array ( [Automotive] => Array ( [0] => Array ( [sub_id] => [cat_id] => 37 [sub_name] => ) ) [Business] => Array ( [0] => Array ( [sub_id] => 27 [cat_id] => 51 [sub_name] => Services ) )
What I have now:
Code: Select all
{foreach from=$categories key='cat_name' item='category'}
{$cat_name}:
{foreach from=$category item='sub_category'}
{$sub_category.sub_name}
{/foreach}
{/foreach}
Posted: Thu Feb 01, 2007 11:23 pm
by gavin1996
Try "recursion"
Posted: Fri Feb 02, 2007 9:14 am
by GeXus
I'm pretty sure there is a more efficient way of doing it... I think recursion has pretty high overhead
Posted: Fri Feb 02, 2007 11:13 am
by RobertGonzalez
Are you looping categories, then looping subcategories that match the category, and trying to make all this span three columns?
Posted: Fri Feb 02, 2007 1:03 pm
by GeXus
Everah wrote:Are you looping categories, then looping subcategories that match the category, and trying to make all this span three columns?
I'm doing one query and assigning it into an array formated like the one above, then I would want it to display like such:
Code: Select all
Category 1 Category 2 Category 3
- Sub 1 - Sub 1 - Sub 1
- Sub 2 - Sub 2 - Sub 2
Category 4 Category 5 Category 6
- Sub 1 - Sub 1 - Sub 1
- Sub 2 - Sub 2 - Sub 2
Basically the same way
http://dmoz.org displays their categories.
Posted: Fri Feb 02, 2007 1:38 pm
by RobertGonzalez
I am sure you can perform some type of template side logic using modulus to achieve what you want. Or you could use a section tag with a skip parameter.
Posted: Fri Feb 02, 2007 1:47 pm
by GeXus
Everah wrote:I am sure you can perform some type of template side logic using modulus to achieve what you want. Or you could use a section tag with a skip parameter.
Someone had mentioned using the 'iteration' property, which will let you know how many times you have iterated through each loop, can you think of how that could be applied to the tr/td's?
I don't think section will handle associated arrays.
Posted: Fri Feb 02, 2007 2:12 pm
by RobertGonzalez
You're right, section handles multidimensional numerical arrays. Sorry. I am a user of TemplateLite myself, and while the syntax for both are similar, there may be differences in what I am telling you...
Is there a relatable category ID of some sort in the array you are building? In that way you could tap into that for checking on the modulus...
Code: Select all
{foreach from=$categories key=k item=v}
{$k}:
{if $v.cat_id % 3 == 0}
{*
This would be the left most column
*}
{elseif $v.cat_id % 3 == 1}
{*
This is the middle column
*}
{elsif $v.cat_id % 3 == 2}
{*
This is the right column
*}
{/if}
{/foreach}
Posted: Fri Feb 02, 2007 3:04 pm
by GeXus
Hmm.. I'm not sure how that would work, this is what I have.. (thanks for your help on this btw!).. I tried using the if statements.. but I really don't know how it should be setup.
Code: Select all
$db->sql_query('SELECT categories.category_id AS cat_id, sub_categories.sub_category_id AS sub_id, categories.name as cat_name, sub_categories.name as sub_name FROM categories LEFT JOIN sub_categories ON (categories.category_id = sub_categories.category_id) ORDER by cat_name');
while ($row = $db->fetch_array()) {
$categories[$row['cat_name']][] = array('sub_id' => $row['sub_id'], 'cat_id' => $row['cat_id'], 'sub_name' => $row['sub_name']);
}
Code: Select all
{foreach from=$categories key='cat_name' item='category'}
<td>
{$cat_name}:
{foreach from=$category item='sub_category'}
{$sub_category.sub_name}
{/foreach}
</td>
{/foreach}
Posted: Fri Feb 02, 2007 3:34 pm
by GeXus
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Ok, I was able to get it to work.. Thank you so much! This is what i did.
[syntax="smarty"]
{foreach from=$categories key='cat_name' item='category' name='cats'}
{if $smarty.foreach.cats.iteration % 3 == 1}
<td>
{$cat_name}:
{foreach from=$category item='sub_category'}
{$sub_category.sub_name}
{/foreach}
</td>
{/if}
{if $smarty.foreach.cats.iteration % 3 == 2}
<td>
{$cat_name}:
{foreach from=$category item='sub_category'}
{$sub_category.sub_name}
{/foreach}
</td>
{/if}
{if $smarty.foreach.cats.iteration % 3 == 0}
<td>
{$cat_name}:
{foreach from=$category item='sub_category'}
{$sub_category.sub_name}
{/foreach}
</tD>
</tr>
<tr>
{/if}
{/foreach}
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Feb 02, 2007 3:54 pm
by RobertGonzalez
Glad you got it worked out.