Jump to content

Traversing A Variable Depth Multidimensional Array!


Recommended Posts

Se post #8.

---

Hi, I'm trying to get a simple list of the names and id:s of subcategories for use in a custom menu.

I've tried this:

>
...
</pre>
<ul>
{foreach from=$subcategories item=subcategory}
getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategory.name|escape:'htmlall':'UTF-8'}
{/foreach}
</ul>
<br>..



Though it won't work unless it's included in category.tpl but I need this menu in new-products.tpl and prices-drop.tpl as well, might be a few more pages later on. I guess it's because CategoryControllerCore class is only availible in category.tpl...
Is there any simple way to do this that I've overlooked.

The custom menu is in a file called prod-menu.tpl which I include when necessary in other tpl-files, maybe I should put it elsewhere?

EDIT: Forgot to mention that I would prefer to get the subcategories in alphabetic order...

Link to comment
Share on other sites

Thanks for the links angora... The last one was just what I needed.

For future reference, here is the code I used to get it to work:

modules/blockcategories/blockcategories.tpl


{l s="Sortiment" mod="blockcategories"}

{foreach from=$blockCategTree.children item=child name=blockCategTree}
{include file="$branche_tpl_path" node=$child}
{/foreach}




modules/blockcategories/category-tree-branch.tpl

{$node.name|escape:'htmlall':'UTF-8'}




I put {$HOOK_LEFT_COLUMN} which I wasn't using for anything in my prod-menu.tpl file and hooked blockcategories module on it.

prod-menu.tpl



{l s="Se våra"}

{l s="Specialpris"}
{l s="Nyheter"}


{$HOOK_LEFT_COLUMN}



css for prod-menu.tpl

#Group1, #Group2
{
margin:20px;
padding:20px;
}

#Group1
{
background-color:#FFDDDD;
}

#Group2
{
background-color:#DDDDDD;
}

#Group1 h2, #Group2 h2
{
font-size:20px;
}

#Group1 li, #Group2 li
{
padding:10px 0 0 10px;
}



Attached a preview... The text is in Swedish and says "Se our", "Special Prices", "News (New Products)" and "Assortment (Catalog)" respectively.

44368_FvfR17ZcKCRWBGWss25P_t

Link to comment
Share on other sites

For sub-sub-categories etc... add the following to the end of:
modules/blockcategories/category-tree-branch.tpl

{if $node.children|@count > 0}
{foreach from=$node.children item=child name=categoryTreeBranch}
{include file="$branche_tpl_path" node=$child}
{/foreach}
{/if}

Link to comment
Share on other sites

What I want is the entire tree of sub, sub-sub, sub-sub-sub, etc... categories to be alphabetized in one long ul list. The use of sub-sub and deeper categories is purely administrative.

Currently I have a category list like this:

Category 1
Category 2
--Category 4
Category 5
Category 3

I tried the ##.Name technique but that didn't work, and the numbers became visible on the page.

I was thinking one might be able to add some code to blockcategories.php to sort the array(s) before returning them, but that code is way to weird for me. I can't figure out what is what in there.

This is the code I have now:

blockcategories.tpl:

{if empty($HOOK_LEFT_COLUMN)}

{l s="Sortiment" mod="blockcategories"}

{foreach from=$blockCategTree.children item=child name=blockCategTree}
{include file="$branche_tpl_path" node=$child}
{/foreach}


{elseif empty($HOOK_RIGHT_COLUMN)}

{l s="Sortiment" mod="blockcategories"}

{foreach from=$blockCategTree.children item=child name=blockCategTree}
{include file="$branche_tpl_path" node=$child}
{/foreach}


{/if}



category-tree-branch.tpl:

{$node.name|escape:'htmlall':'UTF-8'}
{if $node.children|@count > 0}
{foreach from=$node.children item=child name=categoryTreeBranch}
{include file="$branche_tpl_path" node=$child}
{/foreach}
{/if}

Link to comment
Share on other sites

Well I sort of figured it out, though it rather ugly code... I'm not really good with smarty...
The bottom part also splits it into three columns. :)

I'd be grateful if someone figures out a way to shorten the $arr array generation.

blockcategories.tpl

{$arr = array()}
{foreach from=$blockCategTree.children item=child}
{if $child.children}
{foreach from=$child.children item=child2}
{if $child2.children}
{foreach from=$child2.children item=child3}
{if $child3.children}
{foreach from=$child3.children item=child4}
{if $child4.children}
{foreach from=$child4.children item=child5}
{if $child5.children}
{foreach from=$child5.children item=child6}
{if $child6.children}
{foreach from=$child6.children item=child7}
{if $child7.children}
{foreach from=$child7.children item=child8}
{if $child8.children}
{foreach from=$child8.children item=child9}
{if $child9.children}
{foreach from=$child9.children item=child10}
{$arr[$child10.id] = $child10.name}
{/foreach}
{/if}
{$arr[$child9.id] = $child9.name}
{/foreach}
{/if}
{$arr[$child8.id] = $child8.name}
{/foreach}
{/if}
{$arr[$child7.id] = $child7.name}
{/foreach}
{/if}
{$arr[$child6.id] = $child6.name}
{/foreach}
{/if}
{$arr[$child5.id] = $child5.name}
{/foreach}
{/if}
{$arr[$child4.id] = $child4.name}
{/foreach}
{/if}
{$arr[$child3.id] = $child3.name}
{/foreach}
{/if}
{$arr[$child2.id] = $child2.name}
{/foreach}
{/if}
{$arr[$child.id] = $child.name}
{/foreach}
{$list = array()}
{foreach from=$arr key=key item=value}
{if $key != 6}
{$list[$key] = $value}
{/if}
{/foreach}
{if empty($HOOK_LEFT_COLUMN)}

{l s="Sortiment" mod="blockcategories"}

{if $list|@natcasesort}
{foreach from=$list key=id item=name}
{$name|escape:'htmlall':'UTF-8'}
{/foreach}
{/if}


{elseif empty($HOOK_RIGHT_COLUMN)}

{l s="Sortiment" mod="blockcategories"}
{$var = count($list)}
{$col3 = ( ( $var - ( $var % 3 ) ) / 3 )}
{$rest = $var % 3}
{if $rest > 0}
{$col1 = $col3 + 1}
{$col2 = $col3}
{if $rest == 2}
{$col2 = $col1}
{/if}
{else}
{$col1 = $col3}
{$col2 = $col3}
{/if}
{if $list|@natcasesort}
{$i = 0}
{foreach from=$list key=id item=name}
{if $i == 0}

{$name|escape:'htmlall':'UTF-8'}
{elseif $i == $col1 - 1}
{$name|escape:'htmlall':'UTF-8'}

{elseif $i == $col1}

{$name|escape:'htmlall':'UTF-8'}
{elseif $i == $col1 + $col2 - 1}
{$name|escape:'htmlall':'UTF-8'}

{elseif $i == $col1 + $col2}

{$name|escape:'htmlall':'UTF-8'}
{elseif $i == $col1 + $col2 + $col3 - 1}
{$name|escape:'htmlall':'UTF-8'}

{else}
{$name|escape:'htmlall':'UTF-8'}
{/if}
{$i = $i + 1}
{/foreach}
{/if}

{/if}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...