Php multidimensional array in unordered list creating url

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 ...

+7
source share
2 answers

It looks like you are changing the $ link variable inside the foreach loop, so you add item1 to $ link, loop through your subelements and go back to the first iteration and add item2 to the variable ...

replace it

 $link .= "/".$category['url_nl']; 

from

 $insidelink = $link . "/".$category['url_nl']; 

(and change the remaining $ link inside the loop to $ insidelink)

Addendum: This is also true for $ startLevel. Do not change it, use +1 inline:

 echo "<li>".$start." - ".$startingLevel +1. "<a href='$link'>{$category['menu_nl']}</a> ($link)</li>\n"; 
+1
source

Here is an easier way:

$ inarray = your multidimensional array. I used directory_map in codeigniter to get the contents of a directory, including its subdirectories.

 $this->getList($filelist2, $filelist); foreach ($filelist as $key => $val) { echo $val; } function getList($inarray, &$filelist, $prefix='') { foreach ($inarray as $inkey => $inval) { if (is_array($inval)) { $filelist = $this->getList($inval, $filelist, $inkey); } else { if ($prefix) $filelist[] = $prefix . '--' . $inval; else $filelist[] = $inval; } } return $filelist; } 
+1
source

All Articles