Anyone?
Thanks in advance, and let me know if you need any additional info.
I am attempting to turn a multi dimensional array to a multiple nested html navigation menu. I have the jist of it from another answer on SO: solution here.
What I am trying to figure out is how to preserve the top level parent link (and any subsequent child page links) in the url for the next child array. I tried passing in the link to the function when it calls itself to build the array, but that only preserved the most recent parent link.
Example:
Home
About
-Info
--Sub Page
becomes:
home about about/info about/info/subpage
Here is a 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] => About
[link] => about
[target] => _self
)
)
And here is the code I am using:
function create_menu_array($arr, $parent = 0){
$pages = array();
foreach($arr as $page){
if($page->parent == $parent){
$page->sub = isset($page->sub) ? $page->sub : $this->create_menu_array($arr, $page->id);
$pages[] = $page;
}
}
return $pages;
}
function create_menu_html($nav){
$html = '';
foreach($nav as $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>';
}
return $html;
}