当前位置: 首页 > 图文教程 > 网络编程 > ASP > 把无限级分类生成数组

ASP
asp搜索两个以上的词的原理
如何使用花生壳?
asp汉字转换成汉语拼音代码
asp程序实现伪静态的代码
asp处理xml数据的发送、接收类
ASP中使用jmail发送邮件的函数
asp中获取字符串中的时间字符串
ASP按照字数限制自动截取标题内容
asp中将内容生成word文档的函数
用ASP自动清空IE缓存里的内容
asp检测conn.execute是否执行成功的函数
自动获取当前页面URL的ASP函数
ASP采集中获取网页内所需的html代码
asp图片采集并且本地重命名保存的函数
如何使用asp去字符串中的超链接
asp网站通过限制请求防止被采集的函数
asp利用正则去掉字符串中所有html内容
用ASP取出HTML里面的图片地址的函数
fso实现整个文件夹内容的复制到另一个文件夹中
适合所有表的添加、删除、修改的函数

ASP 中的 把无限级分类生成数组


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 96 ::
收藏到网摘: n/a

把无限级分类循环出来确实是一件头疼的事情。
比如,我们要循环出一个SELECT,或一个TABLE,要写一大堆判断。
有没好点的办法呢?
我的做法是生成数组,可以重复调用,直接循环数组就行了。
为了方便,我把它写成了类。
class.asp
返回所有分类的数组,并按顺序排列
有4个属性:
复制代码 代码如下:
set aa=new classlist
aa.id="id"//编号的名称
aa.classname="classname"//分类名称
aa.pid="pid"//父ID名称
aa.db_name="class"//表名
list=aa.arrylist()

复制代码 代码如下:
<%
class classlist
private c_id
private c_db_name
private c_pid
private c_classname
public property let id(str)
c_id = str
end property
public property let db_name(str)
c_db_name = str
end property
public property let pid(str)
c_pid = str
end property
public property let classname(str)
c_classname = str
end property
dim list()
dim i,n
Private Sub Class_Initialize()'初始化变量
i=0
n=0
End Sub
public function classarry(thisid,pid)'取得下级ID
if pid>0 then
sql="select * from "&c_db_name&" where "&c_pid&"="&thisid
else
sql="select * from "&c_db_name&" where "&c_id&"="&thisid
end if
set rs_c=conn.execute(sql)
n=n+1
do while not rs_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
end function
public function arrylist()'循环出所有根类
set rs_c=conn.execute("select count("&c_id&") from "&c_db_name)
lenght=rs_c(0)
rs_c.close
redim list(2,lenght)'设置数组
set rs1=conn.execute("select "&c_id&" from "&c_db_name&" where "&c_pid&"=0")
do while not rs1.eof
call classarry(rs1(c_id),0)
'n=1
rs1.movenext
loop
rs1.close
arrylist=list
end function
end class
%>

实例测试:
表CLASS
字段
id:自动编号
classname:名称
pid:父ID
test.asp
复制代码 代码如下:
<!--#include file="class.asp"-->
<%
Set conn=Server.CreateObject("ADODB.connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
StrDSN = "Driver={Microsoft Access Driver (*.mdb)}; DBQ="
StrDSN = StrDSN & Server.MapPath("test.mdb")
conn.Open strDSN
function ins(num)
str=""
for ii=1 to num
str=str&"|-"
next
ins=str
end function
set aa=new classlist
aa.id="id"
aa.classname="classname"
aa.pid="pid"
aa.db_name="class"
list=aa.arrylist()
response.write "<table border=1><tr><td>ID</td><td>名称</td>< td>第几类</td></tr>"
for j=0 to ubound(list,2)
response.write "<tr><td>"&list(0,j)&"</td><td>"&list(1,j)&"</td><td>"&list(2,j)&"</td></tr>"
next
response.write "</table>"
'response.write list(1,3)
%>
<select name="">
<% for i=0 to ubound(list,2)%>
<option value=""><%
response.write ins(list(2,i))
response.write list(1,i)%></option>
<%next%>
</select>
循环结果:
www.hubro.net/code/class/test.asp
基本上可以满足一般的需要了!