000
25.01.2007, 20:03
chriss
|
Moin Leute,
ich habe mal wieder ein Problem.
Warning: Invalid argument supplied for foreach() in index.php on line 50
Line 50 entspricht dem hier:
<?php foreach($tempmenu as $element) { ?>
Hier das Script:
<?php // Wird quasi aus der Datenbank geholt. (ist nur jetzt fürs Beispiel, DB aufbau ist gleich.) $menu = array( array('id' => 1, 'title' => "Hauptpunkt 1", 'parent_id' => 0), array('id' => 2, 'title' => "Hauptpunkt 2", 'parent_id' => 0), array('id' => 3, 'title' => "Unterpunkt zu 2", 'parent_id' => 2), array('id' => 4, 'title' => "Unterunterpunkt zu 3", 'parent_id' => 3), array('id' => 5, 'title' => "Hauptpunkt 1", 'parent_id' => 0), array('id' => 6, 'title' => "Hauptpunkt 3", 'parent_id' => 0), array('id' => 7, 'title' => "Unterpunkt zu 1", 'parent_id' => 1), array('id' => 8, 'title' => "Unterpunkt zu 4", 'parent_id' => 4) );
$sorted_menu = array();
function sortMenu($id) { global $menu, $sorted_menu;
$tempmenu = $menu;
foreach($tempmenu as $element) { if ($id == $element['parent_id']) { $sorted_menu[] = $element;
sortMenu($element['id']); } } }
// Dann einfach die Funktion mit 0 starten. sortMenu(0);
// Funktionsbeispiel: foreach($sorted_menu as $entry) { echo '<p><a href="#">ID: {' . $entry['id'] . '} - {' . $entry['title'] . '} - {' . $entry['parent_id'] . '}</a></p>'; } ?>
--
Dieser Beitrag wurde am 25.01.2007 um 20:03 von nox bearbeitet.
|
|
Profil || Suche
|
001
25.01.2007, 23:43
theDon
|
Du musst $menu auch auf Dateiebene noch explizit als global deklarieren, damit das funktioniert.
--
\o tanz den naziprau! o/
And more than ever, I hope to never fall, Where enough is not the same it was before
|
|
Profil || Suche
|
002
26.01.2007, 14:00
chriss
|
Funktioniert, danke. Aber irgendwie spricht mich diese Lösung nicht so recht an. Hat jemand eine sauberere Idee?
--
|
|
Profil || Suche
|
003
26.01.2007, 14:58
theDon
|
Ich wuerd das per Parameter uebergeben (braucht dann by-reference, damit das geht):
<?php function sort($id, &$src, &$dst) { /* ... */ } ?>
--
\o tanz den naziprau! o/
And more than ever, I hope to never fall, Where enough is not the same it was before
|
|
Profil || Suche
|