ASP recursive infinite class classification function
Copy Code code as follows:
<%
' Function: getcatagory
' Function: Get category List
' Parameters: Cat_arr-> category Array (rscordset:id: Category number, PID: Superior category, ClassName: Category name, Childs: Sub category)
' Click here to output some SQL statements and get the data with GetRows
' Cat_pid-> the superior category number
' Cat_childs-> sub-category number
' Cat_select-> choice of classification
' Cat_dir-> classification level
' Back: Return to Category List (Option)
Dim Conn,cmd,rs,cat_arr
Set conn = Server.CreateObject ("ADODB. Connection ")
Set cmd = Server.CreateObject ("Adodb.command")
Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "& Server.MapPath (" Db1.mdb ")
Cmd. ActiveConnection = conn
Cmd.commandtext = "SELECT * from Cate ORDER by id DESC"
Set rs = cmd. Execute
Cat_arr = Rs. GetRows ()
Set rs = Nothing
Set cmd = Nothing
Set conn = Nothing
Getcatagory cat_arr,0, "", "", "", "{$cat. Dir}├─<a href=" "id={$cat. ID}" "title=" classification level: {$cat. Dir} category number: {$cat. ID} Category Superior Number: {$cat. PID} category name: {$cat. Name} subcategory: {$cat. Childs} "" >{$cat. Name} </a><br/> "&vbcrlf
Function Getcatagory (ByVal cat_arr,byval Cat_pid,byval cat_childs,byval cat_select,byval cat_dir,byval format)
Dim i,tmp
If IsArray (Cat_arr) Then
For i=0 to UBound (cat_arr,2)
If Cat_arr (1,i) = Cat_pid and InStr ("," & Cat_childs &, ",", "& Cat_arr (0,i) &", ") = 0 Then
TMP = format
If InStr (tmp, "{$cat. Dir}") >0 then TMP = replace (tmp, "{$cat. Dir}", Cat_dir)
If InStr (tmp, "{$cat. ID}") >0 then TMP = replace (tmp, "{$cat. ID}", Cat_arr (0,i))
If InStr (tmp, "{$cat. PID}") >0 then TMP = replace (tmp, "{$cat. pid}", Cat_arr (1,i))
If InStr (tmp, "{$cat. Name}") >0 then TMP = replace (tmp, "{$cat. Name}", Cat_arr (2,i))
If InStr (tmp, "{$cat. Childs}") >0 then TMP = replace (tmp, "{$cat. Childs}", Cat_arr (3,i))
Response.Write tmp
Call Getcatagory (Cat_arr,cat_arr (0,i), Cat_childs,cat_select,cat_dir & "│", format)
End If
Next
End If
End Function
%>
Reproduced a recursive function, a more typical application, there is no special algorithm, at present, we generally common infinite class classification functions are similar. Easy to sort out, including sample packaging Getcatagory.rar
* Big Class 1
└ two grade small class 1
└ three grade small class 1
└ four Grade small class 1
└ five grade small class 1
* Big Class 2
└ two grade small class 2
* Big Class 3
Database Description: Database db.mdb,classtable table structure: ClassID Category ID (auto grow) ParentID Parent ID defaults to 0 (0 represents the highest level) classname category name, Classdepth is to record the series of categories ————— ——————————-
| classid| classname| ParentID | classdepth |
———————————————-
Main code:
Copy Code code as follows:
First Take out the highest (parentid=0) category
<%
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server. MapPath ("Db.mdb")
Set Rs1=server.createobject ("Adodb.recordset")
Sql1= "SELECT * from classtable where parentid=0 order by ClassID"
Rs1.open sql1,conn,1,1
If rs1.eof or Rs1.bof then
Response.Write "not sorted yet!"
Else
While not rs1.eof
Id1=rs1 ("ClassID")
Name1=rs1 ("classname")
Response.Write "*<a href= ' class.asp?id=" &id1& "&name=" &name1& ">" &name1& "</a ><br> "
Parentid1=rs1 ("ParentID")
Call Reclass (ID1)
Rs1.movenext
Wend
End If
Rs1.close
Set rs1=nothing
Sub Reclass (ID)
' Recursive calling function, generating a category code
Set Rs=server.createobject ("Adodb.recordset")
Sql= "SELECT * from classtable where parentid=" &id
Rs.Open sql,conn,1,1
I=1
While not rs.eof
Id0=rs ("ClassID")
Classname0=rs ("classname")
Parentid0=rs ("ParentID")
Classdepth0=rs ("Classdepth")
Brstr= ""
For J=1 to Classdepth0
Brstr= "" &brstr
Next
Response.Write (brstr& "└<a href= ' class.asp?id=" &id0& "&name=" &classname0& "' >" & classname0& "</a><br>")
Call Reclass (ID0)
Rs.movenext
I=i+1
Wend
Rs.close
Set rs=nothing
End Sub
If Request ("a") = "Add" Then
Call add
End If
If Request ("name") <> "then
%>
<table width= "80%" align= "center" cellpadding= "0″cellspacing=" 0″>
<form action= "class.asp?a=add&id=<%=request (" id ")%>" method= "Post >
<tr>
<td> </td>
<td> add small class </td> in <font color= "#FF0000 ″><%=request (" name ")%></font>
</tr>
<tr>
<td> Category name:</td>
<td><input name= "classname" type= "text" id= "classname" ></td>
</tr>
<tr>
<td> </td>
<td><input type= "Submit" name= "Submission" value= "submitted" ></td>
</tr>
</form>
</table>
<%end if
Sub Add ' Add Category
Id=request ("id")
Classname=request ("classname")
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open "Select Parentid,classdepth from classtable where classid=" &id,conn,1,1
Parentid=rs (0)
Classdepth=rs (1) # +1
Rs.close
Set rs=nothing
Sql= "INSERT into classtable (classname,parentid,classdepth) VALUES (' &classname&" ', "&id&", "& classdepth& ")"
Conn.execute SQL
Response. Write "<script>alert (' Add success! '); location.href= ' class.asp ';</script> '
End Sub
%>