PS:今天在帮一个客户写东西,正好用到,就整理放上来了!
二级分类
id 自动编号 所以级别的唯一标识
n_root 文本 类别名称
n_sign 数字 用来标识0是主分类 子类的是存父类的ID
n_order 数字 类别排序(可以没有)
添加主分类:
Codeasp Code if request.Form("sign")="form1" then
n_root=trim(request.Form("n_root"))
n_order=trim(request.Form("n_order"))
set rs=server.CreateObject("adodb.recordset")
sql="select * from sort where n_sign=0 and n_root='"&n_root&"'"
rs.open sql,conn,1,3
if not rs.eof then
call showmsg("这个类别已经存在,\n请换一个名再试")
call goback()
else
rs.addnew
rs("n_root")=n_root
rs("n_order")=n_order
rs.update
response.Write "添加成功<a href='n_addsort.asp'>点击返回</a>"
response.End
end if
rs.close
set rs=nothing
添加二级分类
Codeasp Code if request.Form("sign")="form2" then
n_f_root=request.Form("n_f_root")
n_s_root=trim(request.Form("n_s_root"))
i_order=trim(request.Form("i_order"))
set rs=server.CreateObject("adodb.recordset")
sql="select * from sort where n_sign=" & n_f_root & " and n_root='" & n_s_root & "'"
rs.open sql,conn,3,2
if rs.recordcount>0 then
call showmsg("此分类名称已经存在")
call goback()
else
rs.addnew
rs("n_root")=n_s_root
rs("n_sign")=n_f_root
rs("n_order")=i_order
rs.update
call showmsg("添加成功!")
call goback()
end if
rs.close
set rs=nothing
else
end if
修改主分类
<%
id=request.QueryString("id")
if id="" or not isnumeric(id) then
call showmsg("参数传递错误,或\n此类别已经被删除")
call goback()
else
set rs=server.CreateObject("adodb.recordset")
sql="select * from sort where id="&id
rs.open sql,conn,1,1
if rs.eof then
call showmsg("此类别已经被删除")
call goback()
else
%>
修改子分类
Codeasp Code <%id=request.QueryString("id")
if request.Form("sign")="form2" then
n_f_root=request.Form("n_f_root")
n_s_root=trim(request.Form("n_s_root"))
i_order=trim(request.Form("i_order"))
set rs=server.CreateObject("adodb.recordset")
sql="select * from sort where id="&id
rs.open sql,conn,3,2
rs("n_root")=n_s_root
rs("n_sign")=n_f_root
rs("n_order")=i_order
rs.update
call showmsg("修改成功!")
call goback()
rs.close
set rs=nothing
end if
%>
读取二级分类的时候代码。我觉得这是关键,
JS代码数组,用来存储数据的
Codeasp Code <script language="JavaScript">
<!--
<%
Dim count2,rsClass2,sqlClass2
set rsClass2=server.createobject("adodb.recordset")
sqlClass2="select * from sort "
rsClass2.open sqlClass2,conn,1,1
%>
var subval2 = new Array();
//数组结构:一级根值,二级根值,二级显示值
<%
count2 = 0
do while not rsClass2.eof
%>
subval2[<%=count2%>] = new Array('<%=rsClass2("n_sign")%>','<%=rsClass2("id")%>','<%=rsClass2("n_root")%>')
<%
count2 = count2 + 1
rsClass2.movenext
loop
rsClass2.close
%>
function changeselect1(locationid)
{
document.form1.s2.length = 0;
document.form1.s2.options[0] = new Option('==请选择子分类==','');
for (i=0; i<subval2.length; i++)
{
if (subval2[i][0] == locationid)
{document.form1.s2.options[document.form1.s2.length] = new Option(subval2[i][2],subval2[i][1]);}
}
}
//-->
</script>
下面是表单内容
Codeasp Code <tr>
<%
Dim count1,rsClass1,sqlClass1
set rsClass1=server.createobject("adodb.recordset")
sqlClass1="select * from sort where n_sign=0 order by n_order desc"
rsClass1.open sqlClass1,conn,1,1
%>
<td width="77" align="right" bgcolor="#F1F4FC">所属分类:</td>
<td height="22"><select name="s1" onChange="changeselect1(this.value);">
<option value="0">==请选择主分类==</option>
<%
count1 = 0
do while not rsClass1.eof
response.write"<option value="&rsClass1("id")&">"&rsClass1("n_root")&"</option>"
count1 = count1 + 1
rsClass1.movenext
loop
rsClass1.close
%>
</select></td>
</tr>
<tr>
<td width="77" align="right" bgcolor="#F1F4FC">子分类: </td>
<td height="22"><select name="s2">
<option value="0">==请选择子分类==</option>
</select></td>
</tr>