注册 登录
编程论坛 ASP技术论坛

谁能把该思路的代码实现?

kira007 发布于 2008-08-06 00:16, 795 次点击
有无限分级类表(category),父类与子类间的关系如下:

categoryID  一级父类ID     ParentID=0

           categoryID      二级父类ID     ParentID=一级父类ID

                            categoryID     三级父类ID     ParentID=二级父类ID

                     ....................................(如此类推)..............

                                            categoryID      N级父类ID     ParentID=(N-1)级父类ID                        

'////////////////////////////////////////////////////////////////////////////////////////////////////////////////
现在我的问题是 :通过获取 表单传递过来的 参数 categoryID
如何把 categoryID 所从属的 父类的categoryID      全部找出来?

例如:传递过来的 categoryID 为第三级, 那就要 把 前两级(第一和第二级)的父类的categoryID找出来....如此类推(传递过来的 categoryID 为 n-1 级,就自动把n-2 级的父类categoryID 全部找出来 )

请问该段代码应该如何编写?思路?
3 回复
#2
tianyu1232008-08-06 11:38
运用递归即可 试试
<!--#include file="conn/conn.asp" -->
<%
dim num
num=0
sub fl(ID) 'ID为传递来的值
set rs=server.createobject("adodb.recordset")
sql="select categoryID,ParentID from [category] where categoryID="& ID
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
   num=num+1
   response.write"<tr bgcolor='#FFFFFF'><td width=100 height=30>"& num &"</td><td width=100 height=30>"&rs("ParentID")&"</td></tr>"
   fl(rs("ParentID")) '调用fl()函数,实现递归
end if
end sub
response.write"<table border=0 cellpadding=0 cellspacing=1 bgcolor='#ECECEC'>"
response.write"<tr bgcolor='#FFFFFF'><td width=100 height=30>"
response.write"categoryID</td><td width=100 height=30>ParentID</td></tr>"
fl(5) '调用函数,输出结果
response.write"</table>"
response.write"此ID共包含<font color=red>"& num &"</font>级分类<br>"
%>
#3
laser20082008-08-06 17:55
高手这么多,我怎么办
#4
kira0072008-08-06 18:47
多谢 tianyu123 的帮忙,问题解决了。
1