Re: tree structure class

From: Date: Thu, 11 Oct 2001 16:29:29 +0000
Subject: Re: tree structure class
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-2253@lists.php.net to get a copy of this message
* Wolfram Kriesing wrote: > * this class can be used to step through a tree using [parent], > [child], etc. I have something similar, used in a commercial project, but I planned to release it as open source soon. It uses the well-known array structure of Ulf Wendel's Menu3 class. It is intended to represent a tree menu like WindowsExplorer, including defining icons for each node. It will be extended with a right management (simply a key "rights" in the node with an array as value which defines the rights a user has to have for accessing this menu entry). It saves the node data (which node is opened?) as session data (using PHPLIB's DB class and PHPLIB's Session class with PHP4 sessions). read_categories_into_tree() reads the data from the db and saves it as a Menu3-compatible array for using with showb(). Feel free to use this snippet: <?php class tree_manager { function insert_baum($baum, &$insertarr, $key) { foreach ($insertarr as $k => $v) { if ($k == $key) { /** * found the key, insert it into this array. */ if (isset($insertarr["$k"]["sub"])) { $insertarr["$k"]["sub"] = array_merge($insertarr["$k"]["sub"],$baum); } else { $insertarr["$k"]["sub"] = $baum; } } if (isset($insertarr["$k"]["sub"])) { $this->insert_baum($baum,$insertarr["$k"]["sub"],$key); } } } function showb(&$myvar,$level,$sessvar) { $str = ""; $this->get_b($myvar,$level,$sessvar,$str); print $str; } function get_b(&$myvar,$level,$sessvar,&$str) { $elements = count($myvar); $loop = 0; foreach ($myvar as $k => $v) { $loop++; if (is_array($v)) { if ($level > 0) { for ($i=1; $i<$level; $i++) $str .= "<img border='0' src='/radmin/icons/tree_vertline.gif' width='18' height='18' align='top' alt=''>"; $str .= "<img border='0' src='/radmin/icons/tree_split.gif' width='18' height='18' align='top' alt=''>"; } if (isset($v["sub"])) { if (isset($GLOBALS["$sessvar"]["$k"])) { $trpar="close"; $icon="<img border='0' src='/radmin/icons/tree_collapse.gif' align='top' alt=''>"; } else { $trpar="open"; $icon="<img border='0' src='/radmin/icons/tree_expand.gif' align='top' alt=''>"; } $str .= "<a href=\"".$GLOBALS["PHP_SELF"]."?treeparam=$trpar&item=$k\">$icon</a>"; } else { if ($loop == $elements) { $abschluss = "tree_end.gif"; } else { $abschluss = "tree_split.gif"; } if ($level == 0) { $str .= "<img border='0' alt='' src='/radmin/icons/$abschluss' width='18' height='18'>"; } } /** * user wants to display an icon on this node. */ if (isset($v["icon"])) { $str .= "<img border='0' src='/radmin/icons/".$v["icon"]."' align='top' alt=''>"; } if (isset($v["url"])) { $str .= "<a href=\"".$v["url"]."\""; if (isset($v["target"])) { $str .= " target=\"".$v["target"]."\""; } $str .= ">"; } $str .= $v["title"]; if (isset($v["url"])) { $str .= "</a>"; } $str .= "<br>"; if (isset($v["sub"]) && isset($GLOBALS["$sessvar"]["$k"])) { $this->get_b($v["sub"],$level+1,$sessvar,$str); } } } } function check_openclose($sessvar) { global $item; if ($GLOBALS["treeparam"] == "open") { $GLOBALS["$sessvar"]["$item"] = "set"; } elseif ($GLOBALS["treeparam"] == "close") { unset($GLOBALS["$sessvar"]["$item"]); } } function read_categories_into_tree($parent, &$readarr) { $db = new DB_job; $db1 = new DB_job; $db->query("select * from category where cparent='$parent'"); while ($db->next_record()) { $id = $db->f("cid"); $name = $db->f("cname"); $hash = $id; $readarr["$hash"] = array( "title" => $name, "url" => "catedit.php?id=$id", "target" => "right" ); $db1->query("select count(*) as anz from category where cparent='$id'"); $db1->next_record(); $anz = $db1->f("anz"); if ($anz > 0) { $readarr["$hash"]["sub"] = array(); $this->read_categories_into_tree($id, $readarr["$hash"]["sub"]); } } } } ?> -- PHP- und MySQL-Schulungen. * Kontakt: * Consultingdienstleistungen. * * Managementseminare als Entscheidungshilfe * team@thinkphp.de * zu einem möglichen Einsatz von PHP und MySQL * 0931 / 78 43 804 *

« previous php.pear.dev (#2253) next »