Ik heb de volgende array en functies om een uitklappend navigatie menu te maken:
Als ik buildMenu(12) aanroep, verwacht ik de volgende output:
Maar ik krijg:
Met andere woorden - de functie doorloopt het pad Fruit > Geel > Banaan en stopt er dan mee. De functie komt niet eens toe aan het door de loop halen van Vlees en Drinken - de nodes die nog eronder moeten. Ik zit nu al uren te turen om menuBuild, maar snap niet waarom hij niet gewoon de hele array doorloopt. Wie ziet de voud wel en helpt mij op weg?
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| $nodes[1] = array('pid'=>0, 'title'=>'Home'); $nodes[2] = array('pid'=>1, 'title'=>'Groenten'); $nodes[3] = array('pid'=>1, 'title'=>'Fruit'); $nodes[4] = array('pid'=>1, 'title'=>'Vlees'); $nodes[5] = array('pid'=>2, 'title'=>'Zomer'); $nodes[6] = array('pid'=>2, 'title'=>'Winter'); $nodes[7] = array('pid'=>3, 'title'=>'Rood'); $nodes[8] = array('pid'=>3, 'title'=>'Geel'); $nodes[9] = array('pid'=>7, 'title'=>'Appel'); $nodes[10] = array('pid'=>7, 'title'=>'Kers'); $nodes[11] = array('pid'=>7, 'title'=>'Aardbei'); $nodes[12] = array('pid'=>8, 'title'=>'Banaan'); $nodes[13] = array('pid'=>1, 'title'=>'Drinken'); /** * Get all node id's from current node to rootnode */ function path_to_top($activeItem) { global $nodes; $unfoldItems = array($activeItem); while ($unfoldItems[count($unfoldItems)-1] != 0) $unfoldItems[] = $nodes[$unfoldItems[count($unfoldItems)-1]]['pid']; return $unfoldItems; } /** * Build navigation menu */ function buildMenu($activeItem, $curItem=1) { global $nodes; $unfoldItems = path_to_top($activeItem); $first = true; foreach ($nodes as $itemKey => $item) { if ($item['pid'] == $curItem) { if ($first) { $output.= '<ul>'; $first = false; } $output.= '<li>'.$item['title']; if (in_array($itemKey, $unfoldItems)) $output.= buildMenu($activeItem, $itemKey); $output.= '</li>'; } } if (!$first) $output.= '</ul>'; return $output; } |
Als ik buildMenu(12) aanroep, verwacht ik de volgende output:
code:
1
2
3
4
5
6
7
| Groenten Fruit Rood Geel Banaan Vlees Drinken |
Maar ik krijg:
code:
1
2
3
4
5
| Groenten Fruit Rood Geel Banaan |
Met andere woorden - de functie doorloopt het pad Fruit > Geel > Banaan en stopt er dan mee. De functie komt niet eens toe aan het door de loop halen van Vlees en Drinken - de nodes die nog eronder moeten. Ik zit nu al uren te turen om menuBuild, maar snap niet waarom hij niet gewoon de hele array doorloopt. Wie ziet de voud wel en helpt mij op weg?
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."