Posted by aarondeloach on 03/12/07 04:19
I originally posted:
>I have an array of id=>category:
>
>[018821] => Automotive Parts
>[222033] => Automotive Parts\Domestic
>[205884] => Automotive Parts\Foreign
>[987219] => Building Supplies
>[624668] => Building Supplies\Lumber
>[063964] => Building Supplies\Lumber\Hardwood
>[592999] => Building Supplies\Lumber\Hardwood\Steel
>[987808] => Building Supplies\Lumber\Structural
>[169626] => Building Supplies\Lumber\Treated
>[966554] => Building Supplies\Plumbing
>[329645] => Construction Adhesives
>[621126] => Roofing Supplies
>[400762] => Roofing Supplies\Asphalt Roofing
>[475138] => Roofing Supplies\Metal Roofing
>
>
>What I'm trying to do is loop through the array and create a category
>tree:
>
>Automotive Parts
> Domestic
> Foreign
>Building Supplies
> Lumber
> Hardwood
> Steel
> Structural
> Treated
> Plumbing
>Construction Adhesives
>Roofing Supplies
> Asphalt Roofing
> Metal Roofing
>
Thanks for all the replies. I've created a solution. Maybe it can be
refined, but it's working:
function categories_to_html(&$categories){
global $path;
$temp = array();
$html='<div class="heading">Browse Items</div>';
$first = true;
foreach ($categories as $id => $str) {
$fields = explode('\\',$str);
$current = &$temp;
$space =" ";
$i=0;
foreach ($fields as $field) {
if (!array_key_exists($field, $current)) {
$current[$field] = array();
$html.=(!$first && $i==0 ? ' <br>' : '').
str_repeat($space,$i).($i==0 ? '<b>' : '» ').
'<a href="'.$path.'mod/store.php?filter='.$id.'&sort='.
$_REQUEST['sort'].'">'.$field.'</a>'.
($i==0 ? '</b>' : '')."<br>";
}
$first=false;
$i++;
$current = &$current[$field];
}
}
return $html.'<br> ';
}
Regards,
Aaron
[Back to original message]
|