PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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
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.
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.
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.
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...
{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}
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.
$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']);
}
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}
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]