看看我的递归写法为什么只到两级

wwsh 2005-09-25 10:35:33
function more_list(id)
dim sRec,sSql,classid_more
set sRec=server.createobject("adodb.recordset")
sSql="select id from classname where parent_id='"&id&"'"
sRec.open sSql,Conn,3,1
if not sRec.eof then
while not sRec.eof
classid_more=classid_more&sRec("id")&","
more_list(sRec("id"))
sRec.movenext
wend
end if
more_list=classid_more
sRec.close '关闭记录集
set sRec=nothing '关闭记录集
end function
只能得到两级,三级的就得不到值了,为什么呢?
...全文
277 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hangring 2005-09-26
  • 打赏
  • 举报
回复
好象楼上两位都没有指出错误
我想应该是这边有问题
1. while not sRec.eof
2. classid_more = classid_more&sRec("id") & ","
3. more_list(sRec("id"))
4. sRec.movenext
5. wend

把第3行 并到第二行去 成为这样
classid_more=classid_more&sRec("id")& "," &more_list(sRec("id"))
其他地方和我试的没多大区别
oldmht 2005-09-26
  • 打赏
  • 举报
回复
你这个递归用的可真大气,效率低,还用那么多内存。。。
不如就用一个rs,不要老是创建又删除
找一层,用rs循环,ID列表就放一个字符串变量里面,关闭rs,循环这个串,逐次用这些ID调用本身

如果数据量大的话,不如加2字段,修改的时候生成每一个项目显示的时候的先后顺序和层数保存到这两个字段。然后每次显示的时候,只要一个循环,按原来算好的顺序和层数逐个显示,效率高很多
intereye 2005-09-26
  • 打赏
  • 举报
回复
sRec.close '关闭记录集
set sRec=nothing '关闭记录集

应该是上面这个地方有问题~~~改成下面试试~~
If Not(sRec.Eof AND sRec.Bof) Then
sRec.close '关闭记录集
set sRec=nothing '关闭记录集
End If
hangring 2005-09-25
  • 打赏
  • 举报
回复
以上数据库的设计是这样的
---
id(int) fid(int) fname(char)
1 0 aaaaaa
2 1 bbbbbb
3 2 cccccc
4 3 dddddd
5 4 eeeeee
6 5 ffffff
7 6 gggggg
hangring 2005-09-25
  • 打赏
  • 举报
回复
这样可以正常输出
<%
Dim conn
Set conn = server.CreateObject("adodb.connection")
conn.open "Provider=microsoft.jet.oledb.4.0;data source = " & server.mappath("db1.mdb")
response.write more_list(1)
conn.close
Set conn = nothing

' #
function more_list(id)
dim sRec,sSql,classid_more
set sRec=server.createobject("adodb.recordset")
sSql="select id, fname from classname where fid=" & id
sRec.open sSql,conn,3,1
if not sRec.eof then
while not sRec.eof
classid_more=classid_more & sRec("fname") & "," & more_list(sRec("id"))
sRec.movenext
wend
end if
more_list=classid_more
sRec.close '关闭记录集
set sRec=nothing '关闭记录集
end Function
%>

28,406

社区成员

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

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