php - Create menu from multi dimensional infinite parent child tree -
anyone?
thanks in advance, , allow me know if need additional info.
i attempting turn multi dimensional array multiple nested html navigation menu. have jist of reply on so: solution here.
what trying figure out how preserve top level parent link (and subsequent kid page links) in url next kid array. tried passing in link function when calls build array, preserved recent parent link.
example:
home about -info --sub page
becomes:
home about/info about/info/subpage
here sample array:
array ( [0] => stdclass object ( [id] => 12 [parent] => 11 [name] => sub page [link] => sub_page [target] => _self ) [1] => stdclass object ( [id] => 14 [parent] => 12 [name] => test [link] => test_test [target] => _self ) [2] => stdclass object ( [id] => 9 [parent] => 0 [name] => home [link] => home [target] => _self ) [3] => stdclass object ( [id] => 11 [parent] => 10 [name] => info [link] => info [target] => _self ) [4] => stdclass object ( [id] => 13 [parent] => 10 [name] => test [link] => test [target] => _self ) [5] => stdclass object ( [id] => 10 [parent] => 0 [name] => [link] => [target] => _self ) )
and here code using:
function create_menu_array($arr, $parent = 0){ $pages = array(); foreach($arr $page){ if($page->parent == $parent){ $page->sub = isset($page->sub) ? $page->sub : $this->create_menu_array($arr, $page->id); $pages[] = $page; } } homecoming $pages; } function create_menu_html($nav){ $html = ''; foreach($nav $page){ $html .= '<ul><li>'; $html .= '<a href="' . base_url().$page->link . '" target="'.$page->target.'">' . $page->name . '</a>'; $html .= $this->create_menu_html($page->sub); $html .= '</li></ul>'; } homecoming $html; }
solution recursive function:
example: http://phpfiddle.org/main/code/pmw-7i3
<?php function array_process_for_ids($items) { $new_array = array(); foreach ($items $item) { $new_array[$item['id']] = $item; } homecoming $new_array; } function menu($items) { function menu_recursive($parent_item) { global $items; unset($items[$parent_item['id']]); echo '<div style="padding-left: 15px;">'; echo '- '.$parent_item['name']; foreach ($items $item) { if ($item['parent'] == $parent_item['id']) { menu_recursive($item); } } echo '</div>'; } foreach ($items $item) { if ($item['parent'] == 0) menu_recursive($item); } } $items = array( array( 'id' => 10, 'parent' => 0, 'name' => 'top page 1', 'link' => 'top_page_1', 'target' => '_self' ), array( 'id' => 12, 'parent' => 0, 'name' => 'top page 2', 'link' => 'top_page_2', 'target' => '_self' ), array( 'id' => 25, 'parent' => 10, 'name' => 'sub page 1 of top page 1', 'link' => 'sub_page_1_of_top_page_1', 'target' => '_self' ), array( 'id' => 26, 'parent' => 12, 'name' => 'sub page 1 of top page 2', 'link' => 'sub_page_1_of_top_page_2', 'target' => '_self' ), array( 'id' => 28, 'parent' => 26, 'name' => 'sub page of sub page 1 of top page 2', 'link' => 'sub_page_of_sub_page_1_of_top_page_2', 'target' => '_self' ) ); $items = array_process_for_ids($items); menu($items); ?> php arrays loops multidimensional-array
No comments:
Post a Comment