📄 00000021.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: thinkin (强强), 信区: Linux <BR>标 题: database tree layout <BR>发信站: BBS 水木清华站 (Thu Feb 17 11:02:54 2000) <BR> <BR><?php // -*- C++ -*- <BR>/* Here are the database definitions (for Solid) that i use in this code. <BR> * It should not be hard to adapt it to another database. <BR> */ <BR>/* <BR>CREATE TABLE dirent_types ( <BR> id INTEGER NOT NULL, <BR> icon VARCHAR(50), <BR> name VARCHAR(50), <BR> PRIMARY KEY(id) <BR>); <BR>INSERT INTO dirent_types VALUES(1, 'folderclosed', 'Directory'); <BR>INSERT INTO dirent_types VALUES(2, 'document', 'File'); <BR>CREATE TABLE directory ( <BR> id INTEGER NOT NULL, <BR> parent INTEGER REFERENCES directory(id), <BR> name VARCHAR(200), <BR> icon VARCHAR(50), <BR> type INTEGER REFERENCES dirent_types(id), <BR> url VARCHAR(200), <BR> PRIMARY KEY(id) <BR>); <BR>DROP INDEX directory_idx; <BR>CREATE UNIQUE INDEX directory_idx ON directory(parent, name); <BR>CREATE SEQUENCE dirent_id; <BR>"CREATE PROCEDURE insert_dir_entry <BR> (name VARCHAR, parent INTEGER, type INTEGER) <BR> RETURNS(id INTEGER) <BR>BEGIN <BR> EXEC SQL WHENEVER SQLERROR ABORT; <BR> EXEC SEQUENCE dirent_id.NEXT INTO id; <BR> EXEC SQL PREPARE c_insert <BR> INSERT INTO directory <BR> (id, parent, type, name) <BR> VALUES(?, ?, ?, ?); <BR> EXEC SQL EXECUTE c_insert USING (id, parent, type, name); <BR> EXEC SQL DROP c_insert; <BR>END"; <BR>CALL insert_dir_entry('My Computer', NULL, 1); <BR>CALL insert_dir_entry('Network Neighbourhood', NULL, 1); <BR>CALL insert_dir_entry('lucifer.guardian.no', 2, 1); <BR>CALL insert_dir_entry('rafael.guardian.no', 2, 1); <BR>CALL insert_dir_entry('uriel.guardian.no', 2, 1); <BR>CALL insert_dir_entry('Control Panel', NULL, 1); <BR>CALL insert_dir_entry('Services', 6, 1); <BR>CALL insert_dir_entry('Apache', 7, 2); <BR>CALL insert_dir_entry('Solid Server 2.2', 7, 2); <BR>*/ <BR>function icon($icon, $name = '', $width = 0, $height = 0) { <BR> global $DOCUMENT_ROOT; <BR> $icon_loc = '/pics/menu'; <BR> $file = "$DOCUMENT_ROOT$icon_loc/$icon.gif"; <BR> if (!$width || !$height) { <BR> $iconinfo = getimagesize($file); <BR> if (!$width) { <BR> $width = $iconinfo[0]; <BR> } <BR> if (!$height) { <BR> $height = $iconinfo[1]; <BR> } <BR> } <BR> printf( '<img%s border=0 align=top src="/pics/menu/%s.gif" '. <BR> 'width="%d" height="%d">', $name ? " name=\"$name\"" : '', <BR> $icon, $width, $height); <BR>} <BR>/* <BR> * Displays, recursively, the contents of a tree given a starting <BR> * point. <BR> * <BR> * Parameters: <BR> * $parent - the parent node (not listed in the directory). Node <BR> * 0 is the root node. <BR> * <BR> * $maxdepth (optional) - maximum number of recursion levels. -1 <BR> * (the default value) means no limits. <BR> * <BR> * $ancestors (optional) - an array of the ancestor nodes in the <BR> * current branch of the tree, with the node closest to the <BR> * top at index 0. <BR> * <BR> * Global variables used: <BR> * $child_nodes <BR> * $node_data <BR> * $last_child <BR> * <BR> * Global variables modified: <BR> * The array pointers in $child_nodes will be modified. <BR> */ <BR>function display_directory($parent, $showdepth = 0, $ancestors = false) { <BR> global $child_nodes, $node_data, $last_child; <BR> reset($child_nodes[$parent]); <BR> $size = sizeof($child_nodes[$parent]); <BR> $lastindex = $size - 1; <BR> if (!$ancestors) { <BR> $ancestors = array(); <BR> } <BR> $depth = sizeof($ancestors); <BR> printf( '<div id="node_%d" class="dirEntry" visibility="%s">', <BR> $parent, $showdepth > 0 ? 'show' : 'hide'); <BR> while (list($index, $node) = each($child_nodes[$parent])) { <BR> /* <BR> For each of the uptree nodes: <BR> If an uptree node is not the last one on its depth <BR> of the branch, there should be a line instead of a blank <BR> before this node's icon. <BR> */ <BR> for ($i = 0; $i < $depth; $i++) { <BR> $up_parent = (int)$node_data[$ancestors[$i]][ 'parent']; <BR> $last_node_on_generation = $last_child[$up_parent]; <BR> $uptree_node_on_generation = $ancestors[$i]; <BR> if ($last_node_on_generation == $uptree_node_on_generation) { <BR> icon( "blank"); <BR> } else { <BR> icon( "line"); <BR> } <BR> } <BR> if ($child_nodes[$node]) { // has children, i.e. it is a folder <BR> $conn_icon = "plus"; <BR> $expand = true; <BR> } else { <BR> $conn_icon = "join"; <BR> $expand = false; <BR> } <BR> if ($index == $lastindex) { <BR> $conn_icon .= "bottom"; <BR> } elseif ($depth == 0 && $index == 0) { <BR> $conn_icon .= "top"; <BR> } <BR> if ($expand) { <BR> printf( "<a href=\"javascript:document.layers['node_%d'].visibility= <BR>'show'\">", $node); <BR> } <BR> icon($conn_icon, "connImg_$node"); <BR> if ($expand) { <BR> print( "</a>"); <BR> } <BR> $icon = $node_data[$node][ 'icon']; <BR> if (!$icon) { <BR> $type = $node_data[$node][ 'type']; <BR> $icon = $GLOBALS[ 'dirent_icons'][$type]; <BR> } <BR> icon($icon, "nodeImg_$node"); <BR> $name = $node_data[$node][ 'name']; <BR> printf( '&nbsp;<font size="%d">%s</font><br%c>', -1, $name, 10); <BR> if ($child_nodes[$node]) { <BR> $newdepth = $showdepth; <BR> if ($newdepth > 0) { <BR> $newdepth--; <BR> } <BR> $new_ancestors = $ancestors; <BR> $new_ancestors[] = $node; <BR> display_directory($node, $newdepth, $new_ancestors); <BR> } <BR> } <BR> print( "</div\n>"); <BR>} <BR>function setup_directory($parent, $maxdepth) <BR>{ <BR> global $dirent_icons, $child_nodes, $node_data, $last_child; <BR> $dirent_icons = sql_assoc( 'SELECT id,icon FROM dirent_types'); <BR> $query = 'SELECT id,parent,type,icon,name '. <BR> 'FROM directory '. <BR> 'ORDER BY parent,name'; <BR> $child_nodes = array(); <BR> $node_data = array(); <BR> $res = sql($query); <BR> while (list($id, $parent, $type, $icon, $name) = db_fetch_row($res)) { <BR> $child_nodes[(int)$parent][] = $id; <BR> $node_data[$id] = array( 'id' => $id, <BR> 'parent' => $parent, <BR> 'type' => $type, <BR> 'icon' => $icon, <BR> 'name' => $name); <BR> $last_child[(int)$parent] = $id; <BR> } <BR>} <BR>?> <BR> <BR>-- <BR> <BR>人生到处知何似? <BR> 应似飞鸿踏雪泥。 <BR> 泥上偶然留指爪, <BR> 鸿飞那复计东西! <BR> <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -