一个关于无限级分类递归输出方面的问题

asde2004 2005-10-30 03:15:57
表:
id 自动编号
parentid 父ID
className 分类名称

从数据库中取出得一二维数组:
a.getitem(0, i) id 自动编号
a.getitem(1, i) parentid 父ID
a.getitem(2, i) className 分类名称

用递归法列出:
----------------------------------------------------------
var rows=a.ubound(2)+1;

function test(s) {
var i=0;
while (i<rows) {
if (a.getItem(1, i)==s) {
Response.Write(a.getItem(2, i)+"<br>");
test(a.getItem(0, i));
}
i++;
}
}
---------------------------------------------

问题在于,为了符合W3C标准,需要输出分类树的格式为:
[html]
<ul>
<li>111<ul><li>AAA<ul><li>aaa</li></ul></li><li>BBB</li></ul></li>
<li>222<ul><li>AAA</li><li>BBB</li><li>CCC</li></ul></li>
<li>333</li>
</ul>
[/html]

偶面对递归就头大,已经晕菜了= =|||。
...全文
169 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
公亮 2005-10-30
  • 打赏
  • 举报
回复
经典啊
dh20156 2005-10-30
  • 打赏
  • 举报
回复
SQL的话可以自定义一个函数来实现,参考:
CREATE FUNCTION GetTree()
RETURNS @re table([id] int,[level] int,sid varchar(8000))
AS
BEGIN
declare @l int
set @l=0
insert @re select [pdid],@l,right(10000+[pdid],4)
from [pdepart] where [bid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[pdid],@l,b.sid+right(10000+a.[pdid],4)
from [pdepart] a,@re b
where a.[bid]=b.[id] and b.[level]=@l-1
end
return
END

表pdepart
pdid pdname bid
2 test2 1
6 test3 2
7 test4D 0
1 testD 0

执行SQL:
SELECT a.pdid, REPLICATE(' ', b.[level]*3) + case when b.[level]>0 then '└' else '' end + a.pdname AS pdname FROM pdepart a INNER JOIN GetTree() b ON a.pdid = b.id ORDER BY b.sid

结果:
序号 类别
1 testD
2 └test2
6 └test3
7 test4D
dh20156 2005-10-30
  • 打赏
  • 举报
回复
那就些个递归函数好了。记得大笨狼原来写过一个,你搜索一下。
asde2004 2005-10-30
  • 打赏
  • 举报
回复
谢谢。不过这个方法不太符合我的初衷。= =|||
我打算后台用程序输出,前台用JS控制显示格式这样。= v =
看来想后台一次到位还比较麻烦T v T

28,406

社区成员

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

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