Ich hab eine Menü Funktion. Die Menüeinträge stehen bei mir in ner MySQL Datenbank.
Funktioniert rekursiv und theoretisch über beliebig viele Ebenen.
Als Ausgabe kommt ne Liste raus die nach belieben per CSS gestaltet werden kann...
Ne Eingabemaske existiert dazu noch nicht.. Und die 3 Ebenen... darauf muss man halt selbst achten..
PHP-Code:
<?php
function menu ($table, $path, $indent="1", $prefix="0")
{
global $connection; //Datenbank Verbindung
echo '<ul>';
$entry = '<li style="%s"> <a href="%s%s">%s</a> </li>';
/* Pfad Angaben aufsplitten */
$path_rec = explode(".",$path);
$parentid = $path_rec["0"];
unset($path_rec["0"]);
$path_array = $path_rec;
$path_rec = implode(".",$path_rec);
/* Inhalt der Kategorie aus der Datenbank lesen */
$sql = "SELECT * FROM ".$table." WHERE parentid='".$parentid."' ORDER BY showid ASC;";
if ($debugmode == 1) { echo $sql; };
$result = mysql_query($sql,$connection);
while ($line = mysql_fetch_array($result))
{
/* type == 0 => Kategorie ausgeben */
if ( $line["type"] == 0 )
{
/* Geöffnete Kagetorie ausgeben */
if ($line["id"] == $path_array["1"])
{
/* Parent Kategorie ausgeben */
$uri = "?cat=".$prefix;
if (isset($_GET["p"]))
{
$uri.="&p=".$_GET["p"];
};
printf($entry,
"text-indent: ".($indent * 10 - 10)."px;", $PHP_SELF,$uri,$line["name"]
);
/* REKURSIV AUFRUF! Gibt Inhalt der geöffneten Kind Kategorie aus. */
menu($table, $path_rec, $indent + 1, $prefix.".".$path_array["1"]);
/* REKURSIV AUFRUF! Gibt Inhalt der geöffneten Kind Kategorie aus. */
} else {
/* Nicht geöffnete Kategorien öffnen */
$uri = "?cat=".$prefix.".".$line["id"];
if (isset($_GET["p"]))
{
$uri.="&p=".$_GET["p"];
};
printf($entry,
"text-indent: ".($indent * 10 - 10)."px;", $PHP_SELF,$uri,$line["name"]
);
};
/* type == 1 => Link ausgeben */
} elseif ( $line["type"] == 1 ) {
$url = $line["url"];
$uri = "";
printf($entry,
"text-indent: ".($indent * 10 - 10)."px;", $url,$uri,$line["name"]
);
};
};
echo '</ul>';
};
menu("menu",$_GET["cat"]);
?>
Code:
MySQL Tabelle:
CREATE TABLE `menu` (
`id` int(3) NOT NULL auto_increment,
`name` text NOT NULL,
`parentid` int(2) NOT NULL default '0',
`showid` int(2) NOT NULL default '0',
`type` int(1) NOT NULL default '0',
`url` text NOT NULL,
PRIMARY KEY (`id`)
);
Bedeutung:
name - Menü Eintrag
parentid - Kategorie unter der ein Eintrag angezeigt wird
showid - Danach wird die Struktur sortiert
type - {0;1} 0 = Kategorie | 1 = Link
url - Wenn type = 1 ist dann muss hier der Link eingetragen werden.