phpbar.de logo

Mailinglisten-Archive

[php] Nested Sets

[php] Nested Sets

Jakob Buchgraber jakob.buchgraber at googlemail.com
Fre Nov 9 19:51:35 CET 2007


Hallo Jörn,

Ich habe diesen Ansatz bereits in einem OpenSource Projekt implementiert 
und es funktioniert bis jetzt einwandfrei.
Hier ist der Code der Methode die das implementiert:
<?php
class ppcTreeMenu {
    ...
    public function asList ($listItemFormat) {

        $selectQuery = "SELECT `node`.`name`, `node`.`link`, 
`node`.`alias`, COUNT(*)-1 AS `level` FROM `menu_items` AS `node`, 
`menu_items` AS `parents` " .
                                   "WHERE `node`.`lft` BETWEEN 
`parents`.`lft` AND `parents`.`rgt` AND " .
                                   "`node`.`menuId`={$this->menuId} AND 
`parents`.`menuId`={$this->menuId} GROUP BY `node`.`lft` ORDER BY 
`node`.`lft`";
       
        $prevLevel = -1;
        $list = "";

        foreach ($this->conn->query ($selectQuery, PDO::FETCH_OBJ) as 
$row) {
           
            if ($prevLevel == $row->level) {
                $list .= "\n</li>\n";
            } else if ($prevLevel < $row->level) {
                $list .= "\n<ul>\n";       
            } else if ( ($prevLevel-$row->level) == 1) {
                $list .= "</li>\n</ul>\n</li>\n";
            } else {
                // close the current node
                $list .= "</li>\n";
                // close the levels
                $list .= str_repeat ("</ul>\n</li>\n", 
$prevLevel-$row->level);
            }

            $listItem = $listItemFormat;

            $listItem = str_replace ("%name", $row->name, $listItem);
            $listItem = str_replace ("%link", $row->link, $listItem);
            $listItem = str_replace ("%alias", $row->alias, $listItem);

            $list .= "<li>\n" . $listItem;

            $prevLevel = $row->level;
        }
        $list .= "\n</li>\n";
        $list .= str_repeat ("</ul>\n</li>\n", $prevLevel);
        $list .= "</ul>\n";

        return $list;
    }
}
...
}

Vielleichts hilfts dir ja.

MfG Jakob

?>

php::bar PHP Wiki   -   Listenarchive