递归树出错
下面代码有点问题,是这里的一个老兄贴出来的,原本是递归树直板式,后来我改成下拉式菜单,
往数据库里录入栏目时,现在如下栏目:
ID UperID sortID (自动ID/父ID/级别ID)
--------------------------------
1 0 1
2 1 2
3 2 3
4 3 4
--------------------------------
上面数组调出栏目递归没有问题,但当在已经添加到第四级栏目时再添加第二级栏目:
ID UperID sortID (自动ID/父ID/级别ID)
--------------------------------
1 0 1
2 1 2
3 2 3
4 3 4
5 2 2 <---这里添加为第二级栏目
--------------------------------
调出运行只递归到第三级树,第四级栏目退回到了第三级的下面并排了,为什么?
<%
dim fatherID
function ShowSort(uID,sID)
dim rs,sql,title,i,j,upID,sortID,temp,fatherTree
upID=uID
sortID=sID
sql="select * from ArticleClass where SortID="&sortID&" and UperID="&upID
title=""
set rs = Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
if not(rs.eof and rs.bof) then
for i=0 to rs.recordcount-1
if sortID>1 then
fatherID=upID
fatherTree=ShowFatherLine(upID,sortID-1)
if i = rs.recordcount-1 then
title=""&temp&fatherTree&" ├ "
else
title=""&temp&fatherTree&" ├ "
end if
else
title=""'最高级目录树
end if
Response.Write "<option value='"&rs("id")&","&rs("uperid")&","&rs("sortid")&","&rs("title")&"'>"&title&""&rs("title")&"</option>"
call ShowSort(rs("ID"),sortID+1)'递归
rs.movenext
next
'else
end if
rs.close
set rs=nothing
end function
function isNext(sID,uID)
dim rs,sql
sql="select count(*) from ArticleClass where id>"&sID&" and SortID="&uID
'Response.Write sql
set rs=conn.execute(sql)
isNext = rs(0)
set rs=nothing
end function
function ShowFatherLine(fID,fSortID)
dim rs,sql
if fSortID=1 then'最后
sql="select count(*) from ArticleClass where id>"&fID&" and SortID="&fSortID
set rs=conn.execute(sql)
ShowFatherLine=parentTree(rs(0))
set rs=nothing
else
sql="select count(*) from ArticleClass where id>"&fID&" and SortID="&fSortID
set rs=conn.execute(sql)
ShowFatherLine=ShowFatherLine(fatherID,fSortID-1)&parentTree(rs(0))'递归
sql="select UperID from ArticleClass where id="&fatherID'fatherID全局变量
set rs=conn.execute(sql)
fatherID=rs(0)
set rs=nothing
end if
end function
function parentTree(TN)
if TN=0 then
parentTree=" │"
else
parentTree=""
end if
end function
%>
<SELECT name="ChannelID" id="ChannelID"><% call ShowSort(0,1)%></select>