下面这段代码是前面的php程序调用的一个menu.cls文件,代码如下:
<?php
class treemenu { var $tree = array();
var $expand = array();
var $visible = array();
var $levels = array();
var $urlparams = array();
var $maxlevel;
var $script;
    var $urlparam; var $img_expand = "../../../image/menutree/tree_expand.gif";
var $img_collapse = "../../../image/menutree/tree_collapse.gif";
var $img_line = "../../../image/menutree/tree_vertline.gif";
var $img_split = "../../../image/menutree/tree_split.gif";
var $img_end = "../../../image/menutree/tree_end.gif";
var $img_leaf = "../../../image/menutree/tree_leaf.gif";
var $img_spc = "../../../image/menutree/tree_space.gif"; function treemenu($urlvar,$urlvars,$nodes){
        $this->urlparam = $urlvar;
        $this->urlparams = $urlvars; $this->script = isset($GLOBALS["PATH_INFO"]) ? $GLOBALS["PATH_INFO"] : $GLOBALS["$this->script_NAME"]; $this->maxlevel=0;
$cnt=0; if (is_array($nodes)){
while ($n = current($nodes)){
$this->tree[$cnt][0]=$n["level"];
$this->tree[$cnt][1]="<nobr>".$n["text"]."</nobr>";
$this->tree[$cnt][2]=$n["link"];
$this->tree[$cnt][3]=$n["target"];
$this->tree[$cnt][4]=0;
if ($this->tree[$cnt][0] > $this->maxlevel) $this->maxlevel=$this->tree[$cnt][0];
$cnt++;
next($nodes);
}
} else {
                    die("nodes:".$nodes);
$fd = fopen($nodes, "r");
if ($fd==0) die("treemenu.inc : Unable to open file ".$nodes);
while ($buffer = fgets($fd, 4096)){
$this->tree[$cnt][0]=strspn($buffer,".");
$node=explode("|", rtrim(substr($buffer,$this->tree[$cnt][0])));
$this->tree[$cnt][1]=$node[0];
$this->tree[$cnt][2]=$node[1];
$this->tree[$cnt][3]=$node[2];
$thds->tree[$cnt][4]=0;
if ($this->tree[$cnt][0] > $this->maxlevel) $this->maxlevel=$this->tree[$cnt][0];
$cnt++;
}
fclose($fd);
} for ($i=0; $i<count($this->tree); $i++) {
$this->expand[$i]=0;
$this->visible[$i]=0;
$this->levels[$i]=0;
} /*********************************************
 *  Get Node numbers to expand               *
 *********************************************/ if (!empty($GLOBALS[$urlvar])) $explevels = explode("|",$GLOBALS[$urlvar]); $i=0;
while($i<count($explevels)){
$this->expand[$explevels[$i]]=1;
$i++;
} /*********************************************
 *  Find last nodes of subtrees              *
 *********************************************/ $lastlevel=$this->maxlevel;
for ($i=count($this->tree)-1; $i>=0; $i--){
if ( $this->tree[$i][0] < $lastlevel ){
for ($j=$this->tree[$i][0]+1; $j <= $this->maxlevel; $j++){
$this->levels[$j]=0;
}
} if ( $this->levels[$this->tree[$i][0]]==0 ){
$this->levels[$this->tree[$i][0]]=1;
$this->tree[$i][4]=1;
} else {
$this->tree[$i][4]=0;
}
$lastlevel=$this->tree[$i][0];
}
/*********************************************
 *  Determine visible nodes                  *
 *********************************************/ // all root nodes are always visible
for ($i=0; $i < count($this->tree); $i++) if ($this->tree[$i][0]==1) $this->visible[$i]=1; for ($i=0; $i < count($explevels); $i++){
$n=$explevels[$i];
if ( ($this->visible[$n]==1) && ($this->expand[$n]==1) ){
$j=$n+1;
while ( $this->tree[$j][0] > $this->tree[$n][0] ){
if ($this->tree[$j][0]==$this->tree[$n][0]+1) $this->visible[$j]=1;
$j++;
}
}
}
   }
function show(){
echo $this->getHTMLTable();
}   function getHTMLTable(){ /*********************************************
 *  Output nicely formatted tree             *
 *********************************************/ for ($i=0; $i<$this->maxlevel; $i++) $this->levels[$i]=1; $this->maxlevel++; $html = "<table cellspacing=0 cellpadding=0 border=0 cols=".($this->maxlevel+3)." width=100%>\n";
$html .= "<tr>";
for ($i=0; $i<$this->maxlevel; $i++) $html .= "<td width=16></td>";
$html .= "<td width=100%>&nbsp;</td></tr>\n"; $cnt=0; while ($cnt<count($this->tree)){
if ($this->visible[$cnt]){ /****************************************
 * start new row                        *
 ****************************************/ $html .= "<tr>"; /****************************************
 * vertical lines from higher levels    *
 ****************************************/ $i=0;
while ($i<$this->tree[$cnt][0]-1){
if ($this->levels[$i]==1){
$html .= "<td><a name='$cnt'></a><img src=\"".$this->img_line."\"></td>";
} else {
$html .= "<td><a name='$cnt'></a><img src=\"".$this->img_spc."\"></td>";
}
$i++;
} /****************************************
 * corner at end of subtree or t-split  *
 ****************************************/ if ($this->tree[$cnt][4]==1){
$html .= "<td><img src=\"".$this->img_end."\"></td>";
$this->levels[$this->tree[$cnt][0]-1]=0;
} else {
$html .= "<td><img src=\"".$this->img_split."\"></td>";
$this->levels[$this->tree[$cnt][0]-1]=1;
} /********************************************
 * Node (with subtree) or Leaf (no subtree) *
 ********************************************/ if ($this->tree[$cnt+1][0]>$this->tree[$cnt][0]){ /****************************************
 * Create expand/collapse parameters    *
 ****************************************/ foreach($this->urlparams as $value) { 
      if(!empty($GLOBALS[$value]) && $value <> $this->urlparam) { 
         $otherparams .= $value."=".$GLOBALS[$value]."&"; 
      }
      } $i=0; $params="?".$otherparams.$this->urlparam."=";
while($i<count($this->expand)){
if ( ($this->expand[$i]==1)
&& ($cnt!=$i)
|| ($this->expand[$i]==0 && $cnt==$i)){ $params=$params.$i;
$params=$params."|";
}
$i++;
} if ($this->expand[$cnt]==0){
$html .= "<td><a href=\"".$$this->script.$params."#$cnt\"><img src=\"".$this->img_expand."\" border=no></a></td>";
} else {
$html .= "<td><a href=\"".$$this->script.$params."#$cnt\"><img src=\"".$this->img_collapse."\" border=no></a></td>";
} } else { /*************************
 * Tree Leaf             *
 *************************/ $html .= "<td><img src=\"".$this->img_leaf."\"></td>";
} /****************************************
 * output item text                     *
 ****************************************/ if ($this->tree[$cnt][2]==""){
$html .= "<td colspan=".($this->maxlevel-$this->tree[$cnt][0]).">".$this->tree[$cnt][1]."</td>";
} else {
$html .= "<td colspan=".($this->maxlevel-$this->tree[$cnt][0])."><a href=\"".$this->tree[$cnt][2]."\" target=\"".$this->tree[$cnt][3]."\">".$this->tree[$cnt][1]."</a></td>";
} /****************************************
 * end row                              *
 ****************************************/ $html .= "</tr>\n";
} $cnt++;
} $html .= "</table>\n";
return $html;
}}
?>