I have a multidimensional array in PHP created by the great icio and ftrotter examples (I use the ftrotterrs array in the array variant):
Rotate database result to array
I did this in an unordered list by the width of this method:
public function outputCategories($categories, $startingLevel = 0) { echo "<ul>\n"; foreach ($categories as $key => $category) { if (count($category['children']) > 0) { echo "<li>{$category['menu_nl']}\n"; $this->outputCategories($category['children'], $link , $start, $startingLevel+1); echo "</li>\n"; } else { echo "<li>{$category['menu_nl']}</li>\n"; } } echo "</ul>\n"; }
So far so good.
Now I want to use the url_nl field to create the URL used as links in the menu. The URL should reflect the link level in the tree, adding / url _nl for each step that it has lowered into the tree.
My goal:
- item 1 (has link: /item_1) * subitem 1 (has link: /item_1/subitem_1) * subitem 2 (has link: /item_1/subitem_1) * subsubitem 1 (has link: /item_1/subitem_2/subsubitem_1) - item 2 (has link: /item_2)
table
id id1 (parent id) menu_nl url_nl title_nl etc
What I still have:
public function outputCategories($categories, $link, $start, $startingLevel = 0) { // if start not exists if(!$start) $start = $startingLevel; echo "<ul>\n"; foreach ($categories as $key => $category) { $link.= "/".$category['url_nl']; if($start != $startingLevel) $link = strrchr($link, '/'); if (count($category['children']) > 0) { echo "<li>".$start." - ".$startingLevel. "<a href='$link'>{$category['menu_nl']}</a> ($link)\n"; $this->outputCategories($category['children'], $link , $start, $startingLevel+1); echo "</li>\n"; } else { $start = $startingLevel+1; echo "<li>".$start." - ".$startingLevel. "<a href='$link'>{$category['menu_nl']}</a> ($link)</li>\n"; } } echo "</ul>\n"; }
As you see in the example, I used the url_nl field, which is added recursively, so each level of the list has a link with a path that is used as a URL.
In any case, I am having problems creating these links, since they are not properly reset when cycling through the hierarchical list. After switching to a child in the list, the first one is right, and the second one is not.
I'm stuck here ...