把无限级分类循环出来确实是一件头疼的事情。
比如,我们要循环出一个SELECT,或一个TABLE,要写一大堆判断。
有没好点的办法呢?
我的做法是生成数组,可以重复调用,直接循环数组就行了。
为了方便,我把它写成了类。
class.asp
返回所有分类的数组,并按顺序排列
有4个属性:
复制代码代码如下:setaa=newclasslist
aa.id="id"//编号的名称
aa.classname="classname"//分类名称
aa.pid="pid"//父ID名称
aa.db_name="class"//表名
list=aa.arrylist()
复制代码代码如下:<%
classclasslist
privatec_id
privatec_db_name
privatec_pid
privatec_classname
publicpropertyletid(str)
c_id=str
endproperty
publicpropertyletdb_name(str)
c_db_name=str
endproperty
publicpropertyletpid(str)
c_pid=str
endproperty
publicpropertyletclassname(str)
c_classname=str
endproperty
dimlist()
dimi,n
PrivateSubClass_Initialize()'初始化变量
i=0
n=0
EndSub
publicfunctionclassarry(thisid,pid)'取得下级ID
ifpid>0then
sql="select*from"&c_db_name&"where"&c_pid&"="&thisid
else
sql="select*from"&c_db_name&"where"&c_id&"="&thisid
endif
setrs_c=conn.execute(sql)
n=n+1
dowhilenotrs_c.eof
list(0,i)=rs_c(c_id)'装入数组中
list(1,i)=rs_c(c_classname)
list(2,i)=n
'n=n+1
i=i+1
thisid=classarry(rs_c(c_id),1)'这里递归调用,直到最后一个子类
rs_c.movenext
loop
n=n-1
rs_c.close
endfunction
publicfunctionarrylist()'循环出所有根类
setrs_c=conn.execute("selectcount("&c_id&")from"&c_db_name)
lenght=rs_c(0)
rs_c.close
redimlist(2,lenght)'设置数组
setrs1=conn.execute("select"&c_id&"from"&c_db_name&"where"&c_pid&"=0")
dowhilenotrs1.eof
callclassarry(rs1(c_id),0)
'n=1
rs1.movenext
loop
rs1.close
arrylist=list
endfunction
endclass
%>
实例测试:
表CLASS
字段
id:自动编号
classname:名称
pid:父ID