hoffe du kannst mir helfen
Ich versuche es bei dynamischen Menüsystemen zu machen
aber ich kann die Untermenüaktion nicht richtig machen :( Sie müssen immer zweimal einen Eintrag aus dem Untermenü auswählen, bevor das Untermenü geöffnet bleibt :(
<?php
include "../inc/db_connect.php";
// Select all entries from the menu table
// $result=mysql_query("SELECT id, label, link, parent FROM menu ORDER BY parent, sort, label");
// Create a multidimensional array to conatin a list of items and parents
$sql = <<<SQL
SELECT id, label, link, parent, class
FROM menu
ORDER BY parent, sort, label
SQL;
if(!$result = $mysqli->query($sql)){
die("There was an error running the query [" . $db->error . "]");
}
$menu = array(
"items" => array(),
"parents" => array()
);
// Builds the array lists with data from the menu table
while ($items = mysqli_fetch_assoc($result))
{
// Creates entry into items array with current menu item id ie. $menu["items"][1]
$menu["items"][$items["id"]] = $items;
// Creates entry into parents array. Parents array contains a list of all items with children
$menu["parents"][$items["parent"]][] = $items["id"];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
$menu = call_user_func(modifymenu, $parent,$menu);
$html = "";
if (isset($menu["parents"][$parent]))
{
$html .= "
<ul>n";
foreach ($menu["parents"][$parent] as $itemId)
{
if(!isset($menu["parents"][$itemId]))
{
$html .= "<li class ="".$menu["items"][$itemId]["class"]."">n <a href="?p=".$menu["items"][$itemId]["link"]."">".$menu["items"][$itemId]["label"]."</a>n</li> n";
}
if(isset($menu["parents"][$itemId]))
{
if ($_SESSION["submenu"] == $menu["items"][$itemId]["id"] ) {
$menu["items"][$itemId]["class"] = "active open";
}
$html .= "
<li class ="submenu ".$menu["items"][$itemId]["class"]." ">n <a href="?p=".$menu["items"][$itemId]["link"]."">".$menu["items"][$itemId]["label"]."</a> n";
$html .= buildMenu($itemId, $menu);
$html .= "</li> n";
}
}
$html .= "</ul> n";
}
return $html;
}
echo buildMenu(0, $menu);
function modifymenu ($parent, $menu) {
$ref = isset($_GET["p"]) ? $_GET["p"] : null;
foreach ($menu["parents"][$parent] as $itemId)
{
if($ref == $menu["items"][$itemId]["link"] ) {
$menu["items"][$itemId]["class"] = "active";
if (isset($menu["items"][$itemId]["parent"])) {
$_SESSION["submenu"] = $menu["items"][$itemId]["parent"];
}else{
$_SESSION["submenu"] = "";
}
}
}
return $menu;
}
function modifyparent ($parent, $menu) {
$ref = isset($_GET["p"]) ? $_GET["p"] : null;
foreach ($menu["parents"][$parent] as $itemId)
{
if($ref == $menu["items"][$itemId]["link"] ) {
$menu["items"][$itemId]["class"] = "active";
$_SESSION["submenu"] = $menu["items"][$itemId]["parent"];
}
}
return $menu;
}
Hoffe, dass jemand den Fehler stoppen kann
PS. Ich bin ein Noop bei PHP und ich weiß es, aber es gibt nur einen Weg, um besser zu werden :)
Antworten:
0 für die Antwort № 1Ich würde mit so etwas gehen:
//Call all menu items from database into array
$dbArray = #whatever database code you choose
Sie können die Top-Level-Links mit a trennen parent
Feld in Ihrer Datenbank.
Dann lassen Sie es durch rekursive Funktionen abarbeiten:
function createMenu($dbArray){
foreach($dbArray as $key => $value){
if($parent=="top")
//establish as top link
createLink($key);
if($parent=="$id")
//if parent matches id then add as sub-menu of this top level item
createLink($key);
}
}
function createLink($linkData){
foreach($linkData as $keys => $values){
//add <a> and other per-link items
}
}
Ich weiß, dass dies eine grobe Antwort ist, aber hoffentlich führt Sie dies zu einer einfacheren Lösung!