===== Menü mit Bootstrap und JSON =====
Um ein Menü dynamisch zu erstellen, habe ich mich dazu entschieden die Menüstruktur im JSON Format abzulegen.
Man kann diese in einen String umwandeln und beliebig in eine Datenbank ablegen.
Hier mal ein Beispiel für die JSON Struktur:\\
\\
array
(
[0] => stdClass Object
(
[id] => 15
[title] => Menü1
[http] => http://www.google.de
[customSelect] => 1
[__domenu_params] => stdClass Object
(
)
)
[1] => stdClass Object
(
[id] => 11
[title] => Menü2
[superselect] => 2
[customSelect] => 1
[children] => Array
(
[0] => stdClass Object
(
[id] => 9
[title] => SubMenü2-1
[http] =>
[superselect] => 1
[customSelect] => 2
[__domenu_params] => stdClass Object
(
)
[children] => Array
(
[0] => stdClass Object
(
[id] => 10
[title] => SubMenü2-1-1
[http] =>
[superselect] => 1
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
[children] => Array
(
[0] => stdClass Object
(
[id] => 16
[title] => Submenü2-1-1-1
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
)
[1] => stdClass Object
(
[id] => 19
[title] => Submenü2-1-1-2x
[http] => http://www.google.de
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
)
)
)
)
)
[1] => stdClass Object
(
[id] => 14
[title] => SumMenü2-2
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
[children] => Array
(
[0] => stdClass Object
(
[id] => 17
[title] => doMenu List Item. 1
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
)
[1] => stdClass Object
(
[id] => 18
[title] => doMenu List Item. 2
[customSelect] => select something...
[__domenu_params] => stdClass Object
(
)
)
)
)
)
[__domenu_params] => stdClass Object
(
)
)
[2] => stdClass Object
(
[title] => Menü3
[http] => http://www.test.de
[customSelect] => select something...
[id] => 12
[__domenu_params] => stdClass Object
(
)
)
)
Und hier das Beispiel für das Menü:
/* Beispiel1 JSON */
// $menu ='[{"id":15,"title":"Menü1","customSelect":"1","__domenu_params":{}},{"id":11,"title":"Menü2","http":"","superselect":"2","customSelect":"1","children":[{"id":9,"title":"SubMenü2-1","http":"","superselect":"1","customSelect":"2","__domenu_params":{},"children":[{"id":10,"title":"SubMenü2-1-1","http":"","superselect":"1","customSelect":"select something...","__domenu_params":{}}]},{"id":14,"title":"SumMenü2-2","customSelect":"select something...","__domenu_params":{}}],"__domenu_params":{}},{"title":"Menü3","customSelect":"select something...","id":12,"__domenu_params":{}}]';
/* Beispiel2 JSON */
//$menu = '[{"id":15,"title":"Menü1","customSelect":"1","__domenu_params":{}},{"id":11,"title":"Menü2","http":"","superselect":"2","customSelect":"1","children":[{"id":9,"title":"SubMenü2-1","http":"","superselect":"1","customSelect":"2","__domenu_params":{},"children":[{"id":10,"title":"SubMenü2-1-1","http":"","superselect":"1","customSelect":"select something...","__domenu_params":{},"children":[{"id":16,"title":"Submenü2-1-1-1","customSelect":"select something...","__domenu_params":{}}]}]},{"id":14,"title":"SumMenü2-2","customSelect":"select something...","__domenu_params":{}}],"__domenu_params":{}},{"title":"Menü3","customSelect":"select something...","id":12,"__domenu_params":{}}]';
/* Beispiel3 JSON */
$menu = '[{"id":15,"title":"Menü1","http":"http://www.google.de","customSelect":"1","__domenu_params":{}},{"id":11,"title":"Menü2","superselect":"2","customSelect":"1","children":[{"id":9,"title":"SubMenü2-1","http":"","superselect":"1","customSelect":"2","__domenu_params":{},"children":[{"id":10,"title":"SubMenü2-1-1","http":"","superselect":"1","customSelect":"select something...","__domenu_params":{},"children":[{"id":16,"title":"Submenü2-1-1-1","customSelect":"select something...","__domenu_params":{}},{"id":19,"title":"Submenü2-1-1-2x","http":"http://www.google.de","customSelect":"select something...","__domenu_params":{}}]} ]},{"id":14,"title":"SumMenü2-2","customSelect":"select something...","__domenu_params":{},"children":[{"id":17,"title":"doMenu List Item. 1","customSelect":"select something...","__domenu_params":{}},{"id":18,"title":"doMenu List Item. 2","customSelect":"select something...","__domenu_params":{}}]}],"__domenu_params":{}},{"title":"Menü3","http":"http://www.test.de","customSelect":"select something...","id":12,"__domenu_params":{}}]';
$menu = json_decode($menu); // Array
?>
funktionierendes Beispiel: recursives Menü mit Daten aus JSON Datei
// recursives Ausführen von Untermenüs
function renderChildren(array $children){
foreach($children as $child){
if ( isset($child->children) )
{
echo '";
} else {
$link = ( isset($child->http) ) ? $child->http : "";
echo ''.$child->title.'';
}
}
}