asp查询父节点下所有节点

xingmin 2008-11-21 10:54:29
数据库使用的是access
有一个表名为:T
结构如下:
id ---int
parentid---int 上层的父节点
sname--varchar(50)

如有以下数据:
id parentid sname
1 -1 根节点
2 1 节点1
3 1 节点2
4 2 节点3
5 2 节点4
6 3 节点5
7 3 节点6
7 6 节点7
3 9
9 10

我需要一个可以显示任意一个节点之下的子节点的asp查询代码,显示为一个表单 ,
如查询ID字段的3 ,可以在网页上显示出如下格式

6 3 节点5
7 3 节点6
7 6 节点7
3 9
9 10
这样的结果,就是说查询任意一个id之下的所有节点ID,并且显示出来。同时可以还需要一个控制,就是说可以控制查询深度,深度控制从1-无限,最好是通过传递控制查询的深度值实现,如控制深度的值是0,那么就是无限深度,而具体数字则是控制的深度的层数。

当然显示方面最好有两种显示,如全部显示,和只显示第一层到第4层,其后的层数可以通过点击最后一层节点显示出来。
...全文
199 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingmin 2008-11-25
  • 打赏
  • 举报
回复
递归遍历查询的深度没有办法控制吗
xingmin 2008-11-24
  • 打赏
  • 举报
回复
up
向良玉 2008-11-23
  • 打赏
  • 举报
回复
学习
xingmin 2008-11-23
  • 打赏
  • 举报
回复
up
半山闲人 2008-11-22
  • 打赏
  • 举报
回复
我只知道用递归可以实现,至于查询深度可以在循环中加入一个变量来控制
virgo2008 2008-11-22
  • 打赏
  • 举报
回复

<%Do while not rs.eof%>
<tr>
<td width="1%" height="20" align="left" valign="middle"><img src="Ima/open.gif" onClick="aa('<%=rs("Unit")%>')" style="cursor:hand"></td>
<td width="99%" ><%=rs("Unit")%></td>
</tr>
<tr>
<td colspan="2"><% Call People(rs("Unit"))%></td>
</tr>
<% rs.movenext:loop%>
'*****************************************************递归调用,我自己写的一个,你试着改改吧
<%
Sub People(x)
Set p=Server.CreateObject("Adodb.RecordSet")
sql1="select * from Sheet where Unit='"&x&"'"
p.open sql1,conn,1,1
%>
<table id='<%=x%>' style="display:none">
<% Do while not p.eof %>
<tr>
<td height="20" colspan="3" align="left" valign="middle">
<span style="margin-left:10px"><input name="checkbox" type="checkbox" id="checkbox" value="<%=p("User")%>"><%=p("User")%></span></td>
</tr>
<% p.movenext: loop %>
</table>
<% End Sub %>
xingmin 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 huyingcheng 的回复:]
我只知道用递归可以实现,至于查询深度可以在循环中加入一个变量来控制
[/Quote]

我能够想到的方法是通过累加,但是累加肯定不对,因为这是树形结构
xingmin 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 xzy21com 的回复:]
我刚刚做过类似的,用户要求一个多级分类(其实这本身是一个无限级分类)

<%
function tt(id)
sql="select * from T where parentid="&id
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if rs.eof and rs.bof then
else
do while not rs.eof
response.write rs("sname")&"-->"
tt rs("id")
rs.movenext
loop
end if
rs.close:set rs=nothing
end function

tt(id)'显示…
[/Quote]

可以加上遍历深度控制吗?
scscms太阳光 2008-11-22
  • 打赏
  • 举报
回复
我刚刚做过类似的,用户要求一个多级分类(其实这本身是一个无限级分类)

<%
function tt(id)
sql="select * from T where parentid="&id
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if rs.eof and rs.bof then
else
do while not rs.eof
response.write rs("sname")&"-->"
tt rs("id")
rs.movenext
loop
end if
rs.close:set rs=nothing
end function

tt(id)'显示指定的ID
%>
细节的地方需要自己修改,这只是核心代码
xingmin 2008-11-22
  • 打赏
  • 举报
回复
id cls_name parent_id
1 第一级 0
2 第二级 1
3 第三级 2
4 第四级 3
5 第二级下1 1
6 第二级下2 5
上面是数据库表结构,表名CommonSite_pcls
xingmin 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 myvicy 的回复:]
我发的和你搜到的也没什么大的区别。
深度就是用第二个参数DEEP
<%
deep = 4
sub showpcls(pid,lvl)
Dim rs
dim arrTmp,i
if lvl>deep then exit function
set rs=conn.execute("select id,cls_name,parent_id from CommonSite_pcls where parent_id="&pid)
do while not rs.eof


response.Write("编号:")
response.Write(rs(0))
response.Write("…
[/Quote]

非常感谢,测试时出现错误


代码

<%

sub showpcls(pid,lvl)
deep = 2
Dim rs
dim arrTmp,i
if lvl>deep then exit sub
set rs=conn.execute("select id,cls_name,parent_id from CommonSite_pcls where

parent_id="&pid)

do while not rs.eof


response.Write("编号:")
response.Write(rs(0))
response.Write("名称:")
response.Write(rs(1))
response.Write("父节点编号:")
response.Write(rs(2))


call showpcls(rs("id"),lvl+1)
rs.movenext
loop
rs.close
set rs=nothing

end sub
%>


id cls_name parent_id
1 第一级 0
2 第二级 1
3 第三级 2
4 第四级 3
5 第二级下1 1
6 第二级下2 5



应该显示的结果是

2 第二级 1
3 第三级 2

5 第二级下1 1


可是显示的结果是

编号:2名称:第二级父节点编号:1编号:3名称:第三级父节点编号:2编号:4名称:第四级父节点编号:3编号:5名称:第二级下1父节点编号:1编号:6名称:第二级下2父节点编号:5
修改lvl>deep 为lvl=deep之后,仍然会显示第二级下2


请问应该怎么修改

myvicy 2008-11-22
  • 打赏
  • 举报
回复
我发的和你搜到的也没什么大的区别。
深度就是用第二个参数DEEP
<%
deep = 4
sub showpcls(pid,lvl)
Dim rs
dim arrTmp,i
if lvl>deep then exit function
set rs=conn.execute("select id,cls_name,parent_id from CommonSite_pcls where parent_id="&pid)
do while not rs.eof


response.Write("编号:")
response.Write(rs(0))
response.Write("名称:")
response.Write(rs(1))
response.Write("父节点编号:")
response.Write(rs(2))


call showpcls(rs("id"),lvl+1)
rs.movenext
loop
rs.close
set rs=nothing

end sub
%>
hndth 2008-11-22
  • 打赏
  • 举报
回复
友情up
filec75 2008-11-22
  • 打赏
  • 举报
回复
友情up
xingmin 2008-11-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 virgo2008 的回复:]
VB code
<%Do while not rs.eof%>
<tr>
<td width="1%" height="20" align="left" valign="middle"><img src="Ima/open.gif" onClick="aa('<%=rs("Unit")%>')" style="cursor:hand"></td>
<td width="99%" ><%=rs("Unit")%></td>
</tr>
<tr>
<td colspan="2"><% Call People(rs("Unit"))%></…
[/Quote]
非常感谢

是分成两个文件还是一个文件,<%Do while not rs.eof%>这句的前面的数据库rs对象语句应该怎么写?其中的遍历深度控制是那个?
xingmin 2008-11-21
  • 打赏
  • 举报
回复
比较急,请大家帮帮忙,提供一个完全的源代码,谢谢!
xingmin 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 myvicy 的回复:]
我发的函数里的sql要改成和你数据库相符的表与字段.
[/Quote]


我在网上搜索到了一个
<%
sub showpcls(pid,lvl)
Dim rs
dim arrTmp,i

set rs=conn.execute("select id,cls_name,parent_id from CommonSite_pcls where parent_id="&pid)
do while not rs.eof



response.Write("编号:")
response.Write(rs(0))
response.Write("名称:")
response.Write(rs(1))
response.Write("父节点编号:")
response.Write(rs(2))


call showpcls(rs("id"),lvl+1)
rs.movenext
loop
rs.close
set rs=nothing

end sub
%>


这个可以,请问怎么控制遍历的深度
xingmin 2008-11-21
  • 打赏
  • 举报
回复
汗,还是不行
xingmin 2008-11-21
  • 打赏
  • 举报
回复
select id from trees where pid=',其中ID是自己编号,pid是父节点编号,对不?
myvicy 2008-11-21
  • 打赏
  • 举报
回复
我发的函数里的sql要改成和你数据库相符的表与字段.
加载更多回复(5)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧