It is indeed a headache to loop through infinite levels of classification.
For example, we need to loop out a SELECT or a TABLE and write a lot of judgments.
Is there a better way?
My approach is to generate an array, you can call it repeatedly, and just loop the array directly.
For convenience, I wrote it into a class.
class.asp
Returns an array of all categories and sorts in order
There are 4 attributes:
Copy the code as follows: setaa=newclasslist
aa.id="id"//Number name
aa.classname="classname"//Classification name
aa.pid="pid"//Parent ID name
aa.db_name="class"//Table name
list=aa.arrylist()
Copy the code as follows: <%
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()'Initialize variable
i=0
n=0
EndSub
publicfunctionclassarry(thisid,pid)'Get the lower 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)' load into array
list(1,i)=rs_c(c_classname)
list(2,i)=n
'n=n+1
i=i+1
thisid=classarry(rs_c(c_id),1)' Here is a recursive call until the last subclass
rs_c.movenext
loop
n=n-1
rs_c.close
endfunction
publicfunctionarrylist()' loops out all root classes
setrs_c=conn.execute("selectcount("&c_id&")from"&c_db_name)
length=rs_c(0)
rs_c.close
redimlist(2,lenght)'Set the array
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
arryllist=list
endfunction
endclass
%>
Example test:
Table CLASS
Fields
id: automatic numbering
classname: name
pid: parent ID