Einzelnen Beitrag anzeigen

Benutzerbild von cruiser
cruiser

Registriert seit: 23. Dez 2003
Ort: Königsbrück/Sachsen
455 Beiträge
 
Delphi 7 Enterprise
 
#2

Re: [PHP] Datensätze als Baum darstellen

  Alt 19. Feb 2008, 18:57
Hallo...

ich nehm Wordpress für eine Website und da hab ichs fürs Menü auch so eine Struktur mit einer eignen Funktion geparsed... evtl. hilft dir das ja...

Code:
// special styled menu
function SahaPrintMenu($parent="0", $path=null, $level=0) {
global $wpdb;

if (! isset($path) ) {
$path = explode("/", $_SERVER["REQUEST_URI"]);
array_shift($path);
}

$actpath= "/";
if ($level > 0) {
$before = "<h3 class=\"level".$level."\">";
$after = "</h3>";
for ($i = 0; $i < $level; $i++) {
$actpath .= "$path[$i]/";
}
} else {
$before = "<h2>";
$after = "</h2>";
}

$nosubs = array(
0 => array("mitglieder", "galerien"),
1 => array("galerie-archiv")
); // end $nosubs

$query = "
SELECT post_title,post_name,id
FROM $wpdb->posts
WHERE post_type=\"page\"
AND post_parent=\"$parent\"
AND post_status=\"publish\"
ORDER BY menu_order
";

$result = $wpdb->get_results($query, ARRAY_A);

foreach ($result as $row) {
if (count($wpdb->get_results("SELECT id FROM $wpdb->posts WHERE post_parent=\"$row[id]\"", ARRAY_A))) {
// hat unterpunkte
if (isset($nosubs[$level])) {
// irgendwas soll keine unterpunkte haben
if (in_array($row[post_name], $nosubs[$level])) {
// dieses soll keine unterpunkte haben
if ($row[post_name] == $path[$level]) {
// aktuell
echo $before."<a class=\"currlink\" href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
} else {
// nicht aktuell
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
} else {
// dieses darf unterpunkte haben
if ($row[post_name] == $path[$level]) {
//aktuell
if (isset($path[$level + 1])) {
// pfad geht tiefer
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
} else {
// pfad geht nicht tiefer
echo $before."<a class=\"currlink\" href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
SahaPrintMenu($row[id], $path, $level + 1);
} else {
// nicht aktuell
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
}
} else {
// alles soll Unterpunkte haben
if ($row[post_name] == $path[$level]) {
// aktuell
if (isset($path[$level + 1])) {
// pfad geht tiefer
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
} else {
// pfad geht nicht tiefer
echo $before."<a class=\"currlink\" href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
SahaPrintMenu($row[id], $path, $level + 1);
} else {
// nicht aktuell
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
}
} else {
// hat keine unterpunkte
if ($row[post_name] == $path[$level]) {
// aktuell
echo $before."<a class=\"currlink\" href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
} else {
// nicht aktuell
echo $before."<a href=\"$actpath".$row[post_name]."\">".$row[post_title]."</a>".$after;
}
}
}
}
zugegeben... ist jetzt seeeeehr WordPress-Spezifisch
  Mit Zitat antworten Zitat