array ( [1] => array ( [id] => 1 [parent_id] => 0 [name] => men [slug] => men [status] => 1 ) [2] => array ( [id] => 2 [parent_id] => 1 [name] => shoes [slug] => shoes [status] => 1 ) [3] => array ( [id] => 3 [parent_id] => 2 [name] => sports [slug] => sports [status] => 1 ) )
here function make sidebar menu tree.
function outree($array, $currentparent, $currentlevel = 0, $previouslevel = -1) { foreach ( $array $categoryid => $category) { if ( $currentparent == $category['parent_id']) { if ( $currentlevel > $previouslevel) echo "<ul>"; if ( $currentlevel == $previouslevel) echo "</li>"; echo "<li><a href='/category/{$category['slug']}' title='{$category['name']}'>{$category['name']}</a>"; if ( $currentlevel > $previouslevel) $previouslevel = $currentlevel; $currentlevel++; outree ($array, $categoryid, $currentlevel, $previouslevel); $currentlevel--; } } if ( $currentlevel == $previouslevel) echo "</li></ul>"; } outree($array, 0);
current function call current slug <a href="/category/men">men</a>
. how retrieve or repeat parent slug current list order? this
<a href="/category/men">men</a> <a href="/category/men/shoes">shoes</a> <a href="/category/men/shoes/sports">sports</a>
ok, here's script sink teeth been had few minutes. @ comments! explains in script. honest, should work straight away i've copied array structure, please read , understand it, don't copy , paste blindly, wont learn way (i wouldn't provide full working code, ones pretty hard explain).
<?php /** * heres categories array structure, can in order sort them hierarchical structure in moment */ $categories = array(); $categories[] = array('id'=>5, 'parent_id' => 4, 'name' => 'bedroom wear', 'slug' => 'bwear', 'status' => 1); $categories[] = array('id'=>6, 'parent_id' => 3, 'name' => 'rolex', 'slug' => 'rolex', 'status' => 1); $categories[] = array('id'=>1, 'parent_id' => 0, 'name' => 'men', 'slug' => 'men', 'status' => 1); $categories[] = array('id'=>2, 'parent_id' => 0, 'name' => 'women', 'slug' => 'women', 'status' => 1); $categories[] = array('id'=>3, 'parent_id' => 1, 'name' => 'watches', 'slug' => 'watches', 'status' => 1); $categories[] = array('id'=>4, 'parent_id' => 2, 'name' => 'bras', 'slug' => 'bras', 'status' => 1); $categories[] = array('id'=>7, 'parent_id' => 2, 'name' => 'jackets', 'slug' => 'jackets', 'status' => 1); /** * function takes categories , processes them nice tree array */ function preprocess_categories($categories) { // first of sort categories array parent id! // need parent created before teh children after all? $parent_ids = array(); foreach($categories $k => $cat) { $parent_ids[$k] = $cat['parent_id']; } array_multisort($parent_ids, sort_asc, $categories); /* note: @ point, categories sorted parent_id key */ // $new contains new categories array pass tree function below (nothign fancy here) $new = array(); // $refs contain references (aka points) places in $new array, magic happens! // without references, difficult have random mess of categories , process them cleanly // references, simple access children of children of chilren @ point of loop // each key in array teh category id, , value "children" array of category // set default reference top level categories (parent id = 0) $refs = array(0=>&$new); // loop teh categories (easy peasy) foreach($categories $c) { // need children array can make pointer it, should children categories popup $c['children'] = array(); // create new entry in $new array, using pointer $ref (remember, may 10 levels down, not top level category) hence need use reference/pointer $refs[$c['parent_id']][$c['id']] = $c; // create new reference record category id $refs[$c['id']] = &$refs[$c['parent_id']][$c['id']]['children']; } return $new; } /** * function generates our html categories array have pre-processed */ function tree($categories, $baseurl = '/category/') { $tree = "<ul>"; foreach($categories $category) { $tree .= "<li>"; $tree .= "<a href='".$baseurl.$category['slug']."'>".$category['name']."</a>"; // magci bit, if there children categories, function loops on // , processes children if top level categories // append children main tree string rather tha echoing reason // pass base url plus our category slug "new base url" can build url correctly if(!empty($category['children'])) { $tree .= tree($category['children'], $baseurl.$category['slug'].'/'); } $tree .= "</li>"; } $tree .= "</ul>"; return $tree; } ///echo "<pre>"; print_r(preprocess_categories($categories)); die(); echo tree( preprocess_categories( $categories ) ); ?>
heres pastebin link if pretty coloured code: http://pastebin.com/kvhcuvs3
Comments
Post a Comment