Hoe kan ik een menu script maken met oneindige mogelijkheid tot submenus
Hoe kan ik dat het beste aanpakken?
Mijn database structuur is als volgt:
(tabelnaam van de categorieen) snm_categories
id - het id van de categorie
parent_id - het parent id van de categorie, deze is gelijk aan het id van een bepaalde categorie, hangt een categorie niet onder een andere categorie, dan is het parent_id altijd 1
level - het niveau van de categorie (top level is 1, hangt een categorie onder een topcategorie dan is die 2, hangt die onder een subcategorie dan is hij 3 enzovoort).
title - titel van de categorie
alias - alias van de categorie (de titel maar dan url friendly)
Momenteel haal ik op deze manier alle topcategorieen op:
(snm_content zijn artikelen/producten deze wil ik later ook kunnen hangen onder categorieen maar dat is voor nu even niet relevant)
Code (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
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
<div id="main-menu" class="main-nav zn_mega_wrapper">
<ul id="menu-main-menu" class="main-menu zn_mega_menu">
<li><a href="home">Home</a></li>
<?PHP
$alias = $_GET['alias'];
//Haal alle categorieen en check gelijk of de desbetreffende categorie artikelen onder zich heeft hangen.
$menu = "
SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias
FROM snm_categories cat
LEFT JOIN snm_content cnt
ON cnt.catid = cat.id
WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7)
AND cat.published = 1
AND cat.level = 1
GROUP BY cat.id
ORDER BY cat.rgt ASC";
$menuconn = $conn->query($menu);
//Loop het resultaat
while($menu = $menuconn->fetch_assoc()){
//Code om te checken of de huidige pagina gelijk is aan de alias
if($menu['alias'] == $alias){
$class='current';
} else {
$class = '';
}
$menuresult .= '<li class="'.$class.' menu-item"><a href="'.$menu['cat_alias'].'">'.$menu['cat_title'].'</a>';
$menuresult .= '</li>';
}
echo $menuresult;
?>
<li><a href="contact">Contact</a></li>
</ul>
</div>
<ul id="menu-main-menu" class="main-menu zn_mega_menu">
<li><a href="home">Home</a></li>
<?PHP
$alias = $_GET['alias'];
//Haal alle categorieen en check gelijk of de desbetreffende categorie artikelen onder zich heeft hangen.
$menu = "
SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias
FROM snm_categories cat
LEFT JOIN snm_content cnt
ON cnt.catid = cat.id
WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7)
AND cat.published = 1
AND cat.level = 1
GROUP BY cat.id
ORDER BY cat.rgt ASC";
$menuconn = $conn->query($menu);
//Loop het resultaat
while($menu = $menuconn->fetch_assoc()){
//Code om te checken of de huidige pagina gelijk is aan de alias
if($menu['alias'] == $alias){
$class='current';
} else {
$class = '';
}
$menuresult .= '<li class="'.$class.' menu-item"><a href="'.$menu['cat_alias'].'">'.$menu['cat_title'].'</a>';
$menuresult .= '</li>';
}
echo $menuresult;
?>
<li><a href="contact">Contact</a></li>
</ul>
</div>
Dit was mijn menu code eerst (let niet op de slordigheid, het is meer als referentie hoe ik het eerst opgelost heb):
Code (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<div id="main-menu" class="main-nav zn_mega_wrapper">
<ul id="menu-main-menu" class="main-menu zn_mega_menu">
<li><a href="home">Home</a></li>
<?
$alias = $_GET['alias'];
//Haal alle categorieen en check gelijk of de desbetreffende categorie artikelen onder zich heeft hangen.
$menu = "
SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias
FROM snm_categories cat
LEFT JOIN snm_content cnt
ON cnt.catid = cat.id
WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7)
AND cat.published = 1
AND cat.level = 1
GROUP BY cat.id
ORDER BY cat.rgt ASC";
$menuconn = $conn->query($menu);
//Loop het resultaat
while($menu = $menuconn->fetch_assoc()){
$cat_ids = '';
$subcatmenulijst = '';
$subcats = '';
//Code om te checken of de huidige pagina gelijk is aan de alias
if($menu['alias'] == $alias){
$class='current';
} else {
$class = '';
}
//Stop alle ids van de categorieen in een array
$cat_ids[] = $menu['cat_id'];
//Implodeer ze met commas ertussen zodat ze bruikbaar zijn in een query
if(!empty($cat_ids)) {
$useableids = implode(',', $cat_ids);
}
// Query voor de subcats
$subcatmenu = 'SELECT * FROM snm_categories WHERE parent_id IN ('.$conn->real_escape_string($useableids).') and published = 1 ORDER BY lft';
$subcatmenucon = $conn->query($subcatmenu);
while($subcatmenu = $subcatmenucon->fetch_assoc()){
$subcatids[] = $subcatmenu['id'];
$subsubcatmenulijst = '';
$subsubartmenulijst = '';
if(!empty($subcatids)) {
$useablesubcatids = implode(',', $subcatids);
}
$subcatmenulijst .= '<li>';
$subcatmenulijst .= '<a href="'.$subcatmenu['alias'].'">'.$subcatmenu['title'].'</a>';
//Subsub categorieen
$subsubcatmenu = 'SELECT * FROM snm_categories WHERE parent_id IN ('.$conn->real_escape_string($useablesubcatids).') and published = 1 ORDER BY lft';
$subsubcatmenucon = $conn->query($subsubcatmenu);
while($subsubcatmenu = $subsubcatmenucon->fetch_assoc()){
$subsubcatmenulijst .= '<li><a href="'.$subsubcatmenu['alias'].'">'.$subsubcatmenu['title'].'</a></li>';
}
//Subsub artikelen
$subsubartmenu = '
SELECT ct.id, ct.title, ct.alias as content_alias, ct.ordering, ct.state, ct.catid, cat.alias as cat_alias
FROM snm_content ct
LEFT JOIN snm_categories cat
ON ct.catid = cat.id
WHERE ct.catid IN ('.$conn->real_escape_string($useablesubcatids).')
AND ct.state = 1
ORDER BY ct.ordering';
$subsubartmenucon = $conn->query($subsubartmenu);
while($subsubartmenu = $subsubartmenucon->fetch_assoc()){
$subsubartmenulijst .= '<li><a href="'.$subsubartmenu['cat_alias'].'/'.$subsubartmenu['content_alias'].'">'.$subsubartmenu['title'].'</a></li>';
}
// Wanneer er sub sub categorieen of artikelen zijn, laat deze dan zien
if($subsubcatmenucon->num_rows > 0 OR $subsubartmenucon->num_rows > 0){
$subcatmenulijst .= '<ul class="sub-menu">';
$subcatmenulijst .= $subsubcatmenulijst;
$subcatmenulijst .= $subsubartmenulijst;
$subcatmenulijst .= '</ul>';
}
$subcatmenulijst .= '</li>';
}
// Als
if(!empty($subcatmenulijst)){
$subcats = 'true';
}
//Loop alle categorieen
$menuresult .= '<li class="'.$class.' menu-item"><a href="'.$menu['cat_alias'].'">'.$menu['cat_title'].'</a>';
//Haal alle artikelen op waar het catid gelijk is aan het id van een categorie (binnen bovenstaande loop, zodat het gebeurd voor elke categorie)
$submenu = "SELECT * FROM snm_content WHERE catid = '".$conn->real_escape_string($menu['cat_id'])."' AND state = 1 ORDER BY ordering";
$submenuconn = $conn->query($submenu);
//Als er het id van een artikel niet leeg is (dus als er artikelen onder hangen) OF als er subcategorieeen aanwezig zijn:
if(!empty($menu['content_id']) OR $subcats == 'true'){
$menuresult .= '<ul class="sub-menu">';
//Loop het resultaat
while($submenu = $submenuconn->fetch_assoc()){
$menuresult .= '<li><a href="'.$menu['cat_alias'].'/'.$submenu['alias'].'">'.$submenu['title'].'</a></li>';
}
// Plak de categorieen onder artikelen
$menuresult .= $subcatmenulijst;
$menuresult .= '</ul>';
}
$menuresult .= '</li>';
}
echo $menuresult;
?>
<li><a href="contact">Contact</a></li>
</ul>
</div>
<ul id="menu-main-menu" class="main-menu zn_mega_menu">
<li><a href="home">Home</a></li>
<?
$alias = $_GET['alias'];
//Haal alle categorieen en check gelijk of de desbetreffende categorie artikelen onder zich heeft hangen.
$menu = "
SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias
FROM snm_categories cat
LEFT JOIN snm_content cnt
ON cnt.catid = cat.id
WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7)
AND cat.published = 1
AND cat.level = 1
GROUP BY cat.id
ORDER BY cat.rgt ASC";
$menuconn = $conn->query($menu);
//Loop het resultaat
while($menu = $menuconn->fetch_assoc()){
$cat_ids = '';
$subcatmenulijst = '';
$subcats = '';
//Code om te checken of de huidige pagina gelijk is aan de alias
if($menu['alias'] == $alias){
$class='current';
} else {
$class = '';
}
//Stop alle ids van de categorieen in een array
$cat_ids[] = $menu['cat_id'];
//Implodeer ze met commas ertussen zodat ze bruikbaar zijn in een query
if(!empty($cat_ids)) {
$useableids = implode(',', $cat_ids);
}
// Query voor de subcats
$subcatmenu = 'SELECT * FROM snm_categories WHERE parent_id IN ('.$conn->real_escape_string($useableids).') and published = 1 ORDER BY lft';
$subcatmenucon = $conn->query($subcatmenu);
while($subcatmenu = $subcatmenucon->fetch_assoc()){
$subcatids[] = $subcatmenu['id'];
$subsubcatmenulijst = '';
$subsubartmenulijst = '';
if(!empty($subcatids)) {
$useablesubcatids = implode(',', $subcatids);
}
$subcatmenulijst .= '<li>';
$subcatmenulijst .= '<a href="'.$subcatmenu['alias'].'">'.$subcatmenu['title'].'</a>';
//Subsub categorieen
$subsubcatmenu = 'SELECT * FROM snm_categories WHERE parent_id IN ('.$conn->real_escape_string($useablesubcatids).') and published = 1 ORDER BY lft';
$subsubcatmenucon = $conn->query($subsubcatmenu);
while($subsubcatmenu = $subsubcatmenucon->fetch_assoc()){
$subsubcatmenulijst .= '<li><a href="'.$subsubcatmenu['alias'].'">'.$subsubcatmenu['title'].'</a></li>';
}
//Subsub artikelen
$subsubartmenu = '
SELECT ct.id, ct.title, ct.alias as content_alias, ct.ordering, ct.state, ct.catid, cat.alias as cat_alias
FROM snm_content ct
LEFT JOIN snm_categories cat
ON ct.catid = cat.id
WHERE ct.catid IN ('.$conn->real_escape_string($useablesubcatids).')
AND ct.state = 1
ORDER BY ct.ordering';
$subsubartmenucon = $conn->query($subsubartmenu);
while($subsubartmenu = $subsubartmenucon->fetch_assoc()){
$subsubartmenulijst .= '<li><a href="'.$subsubartmenu['cat_alias'].'/'.$subsubartmenu['content_alias'].'">'.$subsubartmenu['title'].'</a></li>';
}
// Wanneer er sub sub categorieen of artikelen zijn, laat deze dan zien
if($subsubcatmenucon->num_rows > 0 OR $subsubartmenucon->num_rows > 0){
$subcatmenulijst .= '<ul class="sub-menu">';
$subcatmenulijst .= $subsubcatmenulijst;
$subcatmenulijst .= $subsubartmenulijst;
$subcatmenulijst .= '</ul>';
}
$subcatmenulijst .= '</li>';
}
// Als
if(!empty($subcatmenulijst)){
$subcats = 'true';
}
//Loop alle categorieen
$menuresult .= '<li class="'.$class.' menu-item"><a href="'.$menu['cat_alias'].'">'.$menu['cat_title'].'</a>';
//Haal alle artikelen op waar het catid gelijk is aan het id van een categorie (binnen bovenstaande loop, zodat het gebeurd voor elke categorie)
$submenu = "SELECT * FROM snm_content WHERE catid = '".$conn->real_escape_string($menu['cat_id'])."' AND state = 1 ORDER BY ordering";
$submenuconn = $conn->query($submenu);
//Als er het id van een artikel niet leeg is (dus als er artikelen onder hangen) OF als er subcategorieeen aanwezig zijn:
if(!empty($menu['content_id']) OR $subcats == 'true'){
$menuresult .= '<ul class="sub-menu">';
//Loop het resultaat
while($submenu = $submenuconn->fetch_assoc()){
$menuresult .= '<li><a href="'.$menu['cat_alias'].'/'.$submenu['alias'].'">'.$submenu['title'].'</a></li>';
}
// Plak de categorieen onder artikelen
$menuresult .= $subcatmenulijst;
$menuresult .= '</ul>';
}
$menuresult .= '</li>';
}
echo $menuresult;
?>
<li><a href="contact">Contact</a></li>
</ul>
</div>
Gewijzigd op 06/04/2018 11:42:28 door Snelle Jaap
https://crisp.tweakblogs.net/blog/317/formatting-a-multi-level-menu-using-only-one-query.html
Dit is hierin al met een enkele query gerealiseerd, zodat je niet steeds een query in een while() {} hoeft te gebruiken, wat de boel kan vertragen.
Code (php)
Code (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
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
<?php
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
if (isset($menuData['parents'][$parentId]))
{
$html = '<ul>';
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= '<li>' . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= buildMenu($itemId, $menuData);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
// output the menu
echo buildMenu(0, $menuData);
?>
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
if (isset($menuData['parents'][$parentId]))
{
$html = '<ul>';
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= '<li>' . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= buildMenu($itemId, $menuData);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
// output the menu
echo buildMenu(0, $menuData);
?>
En dit werkt met recursie, en dat is ook het ingrediënt die je nodig hebt in zulke situaties.
Gewijzigd op 06/04/2018 11:57:13 door - Ariën -
Bedankt, daar heb ik wat aan! Ik heb het bijna werkend met deze code, enige probleem is dat hij om de toplevel categorieen (met parent_id 1) ook een <ul> gooit, wat niet de bedoeling is. Even kijken hoe ik dat kan fixen.
Zitten je waardes wel op de juiste manier in je array?
Code (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
Array
(
[items] => Array
(
[32] => Array
(
[cat_id] => 32
[level] => 3
[parent_id] => 30
[cat_title] => Subsubcat
[cat_alias] => subsubcat
[published] => 1
[rgt] => 30
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[30] => Array
(
[cat_id] => 30
[level] => 2
[parent_id] => 16
[cat_title] => Subcat
[cat_alias] => subcat
[published] => 1
[rgt] => 31
[state] => 1
[content_id] => 15
[catid] => 30
[content_title] => Subart 1
[content_alias] => subart-1
)
[31] => Array
(
[cat_id] => 31
[level] => 2
[parent_id] => 16
[cat_title] => Subcat 2
[cat_alias] => subcat-2
[published] => 1
[rgt] => 33
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[16] => Array
(
[cat_id] => 16
[level] => 1
[parent_id] => 1
[cat_title] => Baden
[cat_alias] => baden
[published] => 1
[rgt] => 34
[state] => 1
[content_id] => 2
[catid] => 16
[content_title] => Bad 1
[content_alias] => bad1
)
[17] => Array
(
[cat_id] => 17
[level] => 1
[parent_id] => 1
[cat_title] => Meubels
[cat_alias] => meubels
[published] => 1
[rgt] => 36
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[33] => Array
(
[cat_id] => 33
[level] => 2
[parent_id] => 18
[cat_title] => Douchedeuren
[cat_alias] => douchedeuren
[published] => 1
[rgt] => 39
[state] => 1
[content_id] => 17
[catid] => 33
[content_title] => Nisdeur met profiel
[content_alias] => nisdeur-met-profiel
)
[34] => Array
(
[cat_id] => 34
[level] => 2
[parent_id] => 18
[cat_title] => Douchewanden
[cat_alias] => douchewanden
[published] => 1
[rgt] => 41
[state] => 1
[content_id] => 23
[catid] => 34
[content_title] => Walk in douchewand
[content_alias] => walk-in-douchewand
)
[35] => Array
(
[cat_id] => 35
[level] => 2
[parent_id] => 18
[cat_title] => Douchkabines
[cat_alias] => douchkabines
[published] => 1
[rgt] => 43
[state] => 1
[content_id] => 26
[catid] => 35
[content_title] => Vierkante cabine met dubbel swingdeur
[content_alias] => vierkante-cabine-met-swingdeur
)
[36] => Array
(
[cat_id] => 36
[level] => 2
[parent_id] => 18
[cat_title] => Badwand
[cat_alias] => badwand
[published] => 1
[rgt] => 45
[state] => 1
[content_id] => 31
[catid] => 36
[content_title] => badwand
[content_alias] => badwand
)
[18] => Array
(
[cat_id] => 18
[level] => 1
[parent_id] => 1
[cat_title] => Douche
[cat_alias] => douche
[published] => 1
[rgt] => 46
[state] => 1
[content_id] => 5
[catid] => 18
[content_title] => Douche 1
[content_alias] => douche-1
)
[19] => Array
(
[cat_id] => 19
[level] => 1
[parent_id] => 1
[cat_title] => Toilet
[cat_alias] => toilet
[published] => 1
[rgt] => 48
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[20] => Array
(
[cat_id] => 20
[level] => 1
[parent_id] => 1
[cat_title] => Kranen
[cat_alias] => kranen
[published] => 1
[rgt] => 50
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[21] => Array
(
[cat_id] => 21
[level] => 1
[parent_id] => 1
[cat_title] => Tegels
[cat_alias] => tegels
[published] => 1
[rgt] => 52
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[22] => Array
(
[cat_id] => 22
[level] => 1
[parent_id] => 1
[cat_title] => Verwarming
[cat_alias] => verwarming
[published] => 1
[rgt] => 54
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[23] => Array
(
[cat_id] => 23
[level] => 1
[parent_id] => 1
[cat_title] => Accessoires
[cat_alias] => accessoires
[published] => 1
[rgt] => 56
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[38] => Array
(
[cat_id] => 38
[level] => 2
[parent_id] => 37
[cat_title] => wastafels
[cat_alias] => wastafels
[published] => 1
[rgt] => 59
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[37] => Array
(
[cat_id] => 37
[level] => 1
[parent_id] => 1
[cat_title] => wastafels
[cat_alias] => wastafels
[published] => 1
[rgt] => 60
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
)
[parents] => Array
(
[30] => Array
(
[0] => 32
)
[16] => Array
(
[0] => 30
[1] => 31
)
[1] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
[8] => 37
)
[18] => Array
(
[0] => 33
[1] => 34
[2] => 35
[3] => 36
)
[37] => Array
(
[0] => 38
)
)
)
(
[items] => Array
(
[32] => Array
(
[cat_id] => 32
[level] => 3
[parent_id] => 30
[cat_title] => Subsubcat
[cat_alias] => subsubcat
[published] => 1
[rgt] => 30
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[30] => Array
(
[cat_id] => 30
[level] => 2
[parent_id] => 16
[cat_title] => Subcat
[cat_alias] => subcat
[published] => 1
[rgt] => 31
[state] => 1
[content_id] => 15
[catid] => 30
[content_title] => Subart 1
[content_alias] => subart-1
)
[31] => Array
(
[cat_id] => 31
[level] => 2
[parent_id] => 16
[cat_title] => Subcat 2
[cat_alias] => subcat-2
[published] => 1
[rgt] => 33
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[16] => Array
(
[cat_id] => 16
[level] => 1
[parent_id] => 1
[cat_title] => Baden
[cat_alias] => baden
[published] => 1
[rgt] => 34
[state] => 1
[content_id] => 2
[catid] => 16
[content_title] => Bad 1
[content_alias] => bad1
)
[17] => Array
(
[cat_id] => 17
[level] => 1
[parent_id] => 1
[cat_title] => Meubels
[cat_alias] => meubels
[published] => 1
[rgt] => 36
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[33] => Array
(
[cat_id] => 33
[level] => 2
[parent_id] => 18
[cat_title] => Douchedeuren
[cat_alias] => douchedeuren
[published] => 1
[rgt] => 39
[state] => 1
[content_id] => 17
[catid] => 33
[content_title] => Nisdeur met profiel
[content_alias] => nisdeur-met-profiel
)
[34] => Array
(
[cat_id] => 34
[level] => 2
[parent_id] => 18
[cat_title] => Douchewanden
[cat_alias] => douchewanden
[published] => 1
[rgt] => 41
[state] => 1
[content_id] => 23
[catid] => 34
[content_title] => Walk in douchewand
[content_alias] => walk-in-douchewand
)
[35] => Array
(
[cat_id] => 35
[level] => 2
[parent_id] => 18
[cat_title] => Douchkabines
[cat_alias] => douchkabines
[published] => 1
[rgt] => 43
[state] => 1
[content_id] => 26
[catid] => 35
[content_title] => Vierkante cabine met dubbel swingdeur
[content_alias] => vierkante-cabine-met-swingdeur
)
[36] => Array
(
[cat_id] => 36
[level] => 2
[parent_id] => 18
[cat_title] => Badwand
[cat_alias] => badwand
[published] => 1
[rgt] => 45
[state] => 1
[content_id] => 31
[catid] => 36
[content_title] => badwand
[content_alias] => badwand
)
[18] => Array
(
[cat_id] => 18
[level] => 1
[parent_id] => 1
[cat_title] => Douche
[cat_alias] => douche
[published] => 1
[rgt] => 46
[state] => 1
[content_id] => 5
[catid] => 18
[content_title] => Douche 1
[content_alias] => douche-1
)
[19] => Array
(
[cat_id] => 19
[level] => 1
[parent_id] => 1
[cat_title] => Toilet
[cat_alias] => toilet
[published] => 1
[rgt] => 48
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[20] => Array
(
[cat_id] => 20
[level] => 1
[parent_id] => 1
[cat_title] => Kranen
[cat_alias] => kranen
[published] => 1
[rgt] => 50
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[21] => Array
(
[cat_id] => 21
[level] => 1
[parent_id] => 1
[cat_title] => Tegels
[cat_alias] => tegels
[published] => 1
[rgt] => 52
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[22] => Array
(
[cat_id] => 22
[level] => 1
[parent_id] => 1
[cat_title] => Verwarming
[cat_alias] => verwarming
[published] => 1
[rgt] => 54
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[23] => Array
(
[cat_id] => 23
[level] => 1
[parent_id] => 1
[cat_title] => Accessoires
[cat_alias] => accessoires
[published] => 1
[rgt] => 56
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[38] => Array
(
[cat_id] => 38
[level] => 2
[parent_id] => 37
[cat_title] => wastafels
[cat_alias] => wastafels
[published] => 1
[rgt] => 59
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
[37] => Array
(
[cat_id] => 37
[level] => 1
[parent_id] => 1
[cat_title] => wastafels
[cat_alias] => wastafels
[published] => 1
[rgt] => 60
[state] =>
[content_id] =>
[catid] =>
[content_title] =>
[content_alias] =>
)
)
[parents] => Array
(
[30] => Array
(
[0] => 32
)
[16] => Array
(
[0] => 30
[1] => 31
)
[1] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
[8] => 37
)
[18] => Array
(
[0] => 33
[1] => 34
[2] => 35
[3] => 36
)
[37] => Array
(
[0] => 38
)
)
)
Dit is nu mijn html resultaat:
Code (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
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
<ul id="menu-main-menu" class="main-menu zn_mega_menu">
<li><a href="home">Home</a></li>
<ul class="sub-menu">
<li class="menu-item">
<a href="baden">Baden</a>
<ul class="sub-menu">
<li class="menu-item">
<a href="subcat">Subcat</a>
<ul class="sub-menu">
<li class="menu-item"><a href="subsubcat">Subsubcat</a></li>
</ul>
</li>
<li class="menu-item"><a href="subcat-2">Subcat 2</a></li>
</ul>
</li>
<li class="menu-item"><a href="meubels">Meubels</a></li>
<li class="menu-item">
<a href="douche">Douche</a>
<ul class="sub-menu">
<li class="menu-item"><a href="douchedeuren">Douchedeuren</a></li>
<li class="menu-item"><a href="douchewanden">Douchewanden</a></li>
<li class="menu-item"><a href="douchkabines">Douchkabines</a></li>
<li class="menu-item"><a href="badwand">Badwand</a></li>
</ul>
</li>
<li class="menu-item"><a href="toilet">Toilet</a></li>
<li class="menu-item"><a href="kranen">Kranen</a></li>
<li class="menu-item"><a href="tegels">Tegels</a></li>
<li class="menu-item"><a href="verwarming">Verwarming</a></li>
<li class="menu-item"><a href="accessoires">Accessoires</a></li>
<li class="menu-item">
<a href="wastafels">wastafels</a>
<ul class="sub-menu">
<li class="menu-item"><a href="wastafels">wastafels</a></li>
</ul>
</li>
</ul>
<li><a href="contact">Contact</a></li>
</ul>
<li><a href="home">Home</a></li>
<ul class="sub-menu">
<li class="menu-item">
<a href="baden">Baden</a>
<ul class="sub-menu">
<li class="menu-item">
<a href="subcat">Subcat</a>
<ul class="sub-menu">
<li class="menu-item"><a href="subsubcat">Subsubcat</a></li>
</ul>
</li>
<li class="menu-item"><a href="subcat-2">Subcat 2</a></li>
</ul>
</li>
<li class="menu-item"><a href="meubels">Meubels</a></li>
<li class="menu-item">
<a href="douche">Douche</a>
<ul class="sub-menu">
<li class="menu-item"><a href="douchedeuren">Douchedeuren</a></li>
<li class="menu-item"><a href="douchewanden">Douchewanden</a></li>
<li class="menu-item"><a href="douchkabines">Douchkabines</a></li>
<li class="menu-item"><a href="badwand">Badwand</a></li>
</ul>
</li>
<li class="menu-item"><a href="toilet">Toilet</a></li>
<li class="menu-item"><a href="kranen">Kranen</a></li>
<li class="menu-item"><a href="tegels">Tegels</a></li>
<li class="menu-item"><a href="verwarming">Verwarming</a></li>
<li class="menu-item"><a href="accessoires">Accessoires</a></li>
<li class="menu-item">
<a href="wastafels">wastafels</a>
<ul class="sub-menu">
<li class="menu-item"><a href="wastafels">wastafels</a></li>
</ul>
</li>
</ul>
<li><a href="contact">Contact</a></li>
</ul>
Baden bijvoorbeeld is een topcategorie met parent_id 1, hier hoeft dus geen <ul> omheen.
De 1 geef ik mee (in het voorbeeld script staat namelijk 0, bij mij is 1 het topniveau).
Op deze manier:
Code (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
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
<?PHP
// Maak een nieuwe array om te vullen met onderstaand resultaat
$menuData = array(
'items' => array(),
'parents' => array()
);
// Maak een nieuwe array met simpelweg items en parents, welke gekoppeld zitten aan cat_id/parent_id
while($menu = $menuconn->fetch_assoc())
{
$menuData['items'][$menu['cat_id']] = $menu;
$menuData['parents'][$menu['parent_id']][] = $menu['cat_id'];
}
// functie om menu te maken, $parentId is 1 (de categorieen die geen parent hebben)
function buildMenu($parentId, $menuData)
{
$html = '';
if (isset($menuData['parents'][$parentId]))
{
//print_r($menuData);
$html = '<ul class="sub-menu">';
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= '<li class="menu-item"><a href="'. $menuData['items'][$itemId]['cat_alias'].'">' . $menuData['items'][$itemId]['cat_title'].'</a>';
// find childitems recursively
$html .= buildMenu($itemId, $menuData);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
// Echo het resultaat van de functie en geef 1 mee als parent_id
echo buildMenu(1, $menuData);
?>
// Maak een nieuwe array om te vullen met onderstaand resultaat
$menuData = array(
'items' => array(),
'parents' => array()
);
// Maak een nieuwe array met simpelweg items en parents, welke gekoppeld zitten aan cat_id/parent_id
while($menu = $menuconn->fetch_assoc())
{
$menuData['items'][$menu['cat_id']] = $menu;
$menuData['parents'][$menu['parent_id']][] = $menu['cat_id'];
}
// functie om menu te maken, $parentId is 1 (de categorieen die geen parent hebben)
function buildMenu($parentId, $menuData)
{
$html = '';
if (isset($menuData['parents'][$parentId]))
{
//print_r($menuData);
$html = '<ul class="sub-menu">';
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= '<li class="menu-item"><a href="'. $menuData['items'][$itemId]['cat_alias'].'">' . $menuData['items'][$itemId]['cat_title'].'</a>';
// find childitems recursively
$html .= buildMenu($itemId, $menuData);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
// Echo het resultaat van de functie en geef 1 mee als parent_id
echo buildMenu(1, $menuData);
?>
Toevoeging op 06/04/2018 12:46:59:
Ik heb het opgelost door te checken of het parent_id gelijk is aan 1, zo ja gooi er een li omheen ipv een ul. Bedankt voor het op weg helpen!
Gewijzigd op 06/04/2018 12:40:05 door Snelle Jaap
Enne, dat $html .= ..., dat kan ook makkelijker he? Gebruik output buffering :).
Gewijzigd op 06/04/2018 15:55:44 door Thomas van den Heuvel