Ik heb een recursieve functie geschreven die eea uit een database haalt en hiermee een option-list construeert:
Met de volgende functie schrijf ik eea uiteindelijk naar het scherm:
Maar wat ik eigenlijk zou willen is een variabele $output aanmaken en hierin de HTML voor de select-list opslaan:
Dan kan ik vervolgens de list oproepen door
Het probleem is alleen dat dit niet werkt - de functie geeft helemaal niets terug. Wat doe ik verkeerd? De oplossing moet hem volgens mij zitten in iets als
Maar dit levert hertzelfde resultaat op. Wie helpt mij aan de oplossing?
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
| function build_category_tree($parent=0, $level=0) { $level++; $query = 'SELECT cc.category_id, cc.parent_id, c.category_id, c.name FROM categories c, categories_categories cc WHERE cc.category_id = c.category_id AND cc.parent_id = ' . $parent; $categories = sql_query($query, 0); $num_categories = mysql_num_rows($categories); for ($i = 0; $i < $num_categories; $i++) { $row = mysql_fetch_array($categories); echo '<option value="' . $row[category_id] . '">'; for ($a = 0; $a < $level; $a++) { if ($a != ($level - 1)) { echo ' '; } else { echo ''; } } echo '| ' . $level . ' | ' . $row[name]; echo '</option>'; if ($row[category_id] != $parent) { build_category_tree($row[category_id], $level); } } } |
Met de volgende functie schrijf ik eea uiteindelijk naar het scherm:
PHP:
1
| build_category_tree(); |
Maar wat ik eigenlijk zou willen is een variabele $output aanmaken en hierin de HTML voor de select-list opslaan:
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
| $output = ''; function build_category_tree($parent=0, $level=0) { $level++; $query = 'SELECT cc.category_id, cc.parent_id, c.category_id, c.name FROM categories c, categories_categories cc WHERE cc.category_id = c.category_id AND cc.parent_id = ' . $parent; $categories = sql_query($query, 0); $num_categories = mysql_num_rows($categories); for ($i = 0; $i < $num_categories; $i++) { $row = mysql_fetch_array($categories); $output .= '<option value="' . $row[category_id] . '">'; for ($a = 0; $a < $level; $a++) { if ($a != ($level - 1)) { echo ' '; } else { echo ''; } } $output .= '| ' . $level . ' | ' . $row[name]; $output .= '</option>'; if ($row[category_id] != $parent) { build_category_tree($row[category_id], $level); } } return $output; } |
Dan kan ik vervolgens de list oproepen door
PHP:
1
| echo $output; |
Het probleem is alleen dat dit niet werkt - de functie geeft helemaal niets terug. Wat doe ik verkeerd? De oplossing moet hem volgens mij zitten in iets als
PHP:
1
2
3
4
5
6
| $output = ''; function build_category_tree($output, $parent=0, $level=0) { global $output; .... |
Maar dit levert hertzelfde resultaat op. Wie helpt mij aan de oplossing?
"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."