给你一个我自己写的perl递归程序
#!/usr/bin/perl -w
use DBI;
$datasrc='mysql:database=vpn';
$username="root";
$password="";
$dbh=DBI->connect("dbi:$datasrc",$username,$password) or die DBI->errstr;$ID =1; #区分显示的Menu类
$layer=1; #菜单的层数
$fatherid=0; #父id
$last_flag=0; #当前层的标识(0:不是当前层最后一个菜单,1:当前层最后一个菜单)
$result=$dbh->prepare("select groupid,fatherid,vpndesc from groupvpn where fatherid=$fatherid");
$result->execute();
&ShowTreeMenu($result,$fatherid,$last_flag);
$dbh->disconnect or warn "Disconnection failed".$DBI->errstr;sub ShowTreeMenu {
my ($result,$fatherid,$last_flag) = @_;
print "<table width=\"100%\" height='20' border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
$result2=$dbh->prepare("select count(*) from groupvpn where fatherid=$fatherid");
$result2->execute();
@array_row=$result2->fetchrow_array;
my $rows=$array_row[0];
my $row=0;
while(@data=$result->fetchrow_array)
{
$row++;
$last1=0;
if($row>=$rows) {
$last1=1;
}
$result1=$dbh->prepare("select count(*) from groupvpn where fatherid=$data[0]");
$result1->execute();
@array=$result1->fetchrow_array;
print "<tr>";
for($i=1;$i<$layer-1;$i++) {
if($i==$layer-2) {
if($last_flag==1) {
print "<td width='10'> </td>";
}
else {
print "<td width='10'><img src='images/vline.gif' border='0'></td>";
}
}
else {
print "<td width='10'><img src='images/vline.gif' border='0'></td>";
}
}
for(;$i<$layer;$i++) {
if($row<$rows) {
print "<td width='10'><img src='images/midblk.gif' border='0'></td>";
}
elsif($row>=$rows){
print "<td width='10'><img src='images/lline.gif' border='0'></td>";
}
}
if($array[0]>0) {
print "<td width='10'><img src='images/folder_close.gif' border='0'></td>";
print "<td width='85%'><a href='vpn/index.cgi?groupid=$data[0]' target='mainframe' onClick='javascript:ShowMenu(Menu$ID);'>$data[2]</a></td>";
}
else {
print "<td width='10'><img src='images/folder_nadir.gif' border='0'></td>";
print "<td width='85%'><a href='vpn/index.cgi?groupid=$data[0]' target='mainframe'>$data[2]</a></td>";
}
print "</tr>";
if($array[0]>0) {
$result1=$dbh->prepare("select groupid,fatherid,vpndesc from groupvpn where fatherid=$data[0]");
$result1->execute();
$colspan=$layer+1;
print "<tr id='Menu$ID' style='display:none'>";
print "<td colspan='$colspan'>";
$ID++;
$layer++;
ShowTreeMenu($result1,$data[0],$last1);
$layer--;
print "</td></tr>";
}
}
print "</table>";
}#程序操作的数据表(Mysql:database:vpn tablename:groupvpn)
#+----------+------------------+------+-----+---------+----------------+
#| Field | Type | Null | Key | Default | Extra |
#+----------+------------------+------+-----+---------+----------------+
#| groupid | int(10) unsigned | | PRI | NULL | auto_increment |
#| fatherid | int(10) unsigned | | | 0 | |
#| deepth | int(11) | YES | | NULL | |
#| policy | varchar(10) | YES | | NULL | |
#| vpndesc | varchar(40) | YES | | NULL | |
#+----------+------------------+------+-----+---------+----------------+
#!/usr/bin/perl -w
use DBI;
$datasrc='mysql:database=vpn';
$username="root";
$password="";
$dbh=DBI->connect("dbi:$datasrc",$username,$password) or die DBI->errstr;$ID =1; #区分显示的Menu类
$layer=1; #菜单的层数
$fatherid=0; #父id
$last_flag=0; #当前层的标识(0:不是当前层最后一个菜单,1:当前层最后一个菜单)
$result=$dbh->prepare("select groupid,fatherid,vpndesc from groupvpn where fatherid=$fatherid");
$result->execute();
&ShowTreeMenu($result,$fatherid,$last_flag);
$dbh->disconnect or warn "Disconnection failed".$DBI->errstr;sub ShowTreeMenu {
my ($result,$fatherid,$last_flag) = @_;
print "<table width=\"100%\" height='20' border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
$result2=$dbh->prepare("select count(*) from groupvpn where fatherid=$fatherid");
$result2->execute();
@array_row=$result2->fetchrow_array;
my $rows=$array_row[0];
my $row=0;
while(@data=$result->fetchrow_array)
{
$row++;
$last1=0;
if($row>=$rows) {
$last1=1;
}
$result1=$dbh->prepare("select count(*) from groupvpn where fatherid=$data[0]");
$result1->execute();
@array=$result1->fetchrow_array;
print "<tr>";
for($i=1;$i<$layer-1;$i++) {
if($i==$layer-2) {
if($last_flag==1) {
print "<td width='10'> </td>";
}
else {
print "<td width='10'><img src='images/vline.gif' border='0'></td>";
}
}
else {
print "<td width='10'><img src='images/vline.gif' border='0'></td>";
}
}
for(;$i<$layer;$i++) {
if($row<$rows) {
print "<td width='10'><img src='images/midblk.gif' border='0'></td>";
}
elsif($row>=$rows){
print "<td width='10'><img src='images/lline.gif' border='0'></td>";
}
}
if($array[0]>0) {
print "<td width='10'><img src='images/folder_close.gif' border='0'></td>";
print "<td width='85%'><a href='vpn/index.cgi?groupid=$data[0]' target='mainframe' onClick='javascript:ShowMenu(Menu$ID);'>$data[2]</a></td>";
}
else {
print "<td width='10'><img src='images/folder_nadir.gif' border='0'></td>";
print "<td width='85%'><a href='vpn/index.cgi?groupid=$data[0]' target='mainframe'>$data[2]</a></td>";
}
print "</tr>";
if($array[0]>0) {
$result1=$dbh->prepare("select groupid,fatherid,vpndesc from groupvpn where fatherid=$data[0]");
$result1->execute();
$colspan=$layer+1;
print "<tr id='Menu$ID' style='display:none'>";
print "<td colspan='$colspan'>";
$ID++;
$layer++;
ShowTreeMenu($result1,$data[0],$last1);
$layer--;
print "</td></tr>";
}
}
print "</table>";
}#程序操作的数据表(Mysql:database:vpn tablename:groupvpn)
#+----------+------------------+------+-----+---------+----------------+
#| Field | Type | Null | Key | Default | Extra |
#+----------+------------------+------+-----+---------+----------------+
#| groupid | int(10) unsigned | | PRI | NULL | auto_increment |
#| fatherid | int(10) unsigned | | | 0 | |
#| deepth | int(11) | YES | | NULL | |
#| policy | varchar(10) | YES | | NULL | |
#| vpndesc | varchar(40) | YES | | NULL | |
#+----------+------------------+------+-----+---------+----------------+
演示:
http://www.adr.gov.cn/download/tree/nolimited.asp
http://www.ymdg.com/tree.htm
www.adr.gov.cn/download/tree.rar下载原文件
关于这个问题的争论:
http://expert.csdn.net/Expert/topic/2442/2442101.xml?temp=6.592959E-02
阿信的树:
www.adr.gov.cn/download/deeptree.rar下载原文件
http://www.9499.net?go=tc