ASP to generate an array of infinite class classification

Source: Internet
Author: User
Tags array table name
Array

It's really a headache to loop through the infinite classes.
For example, we have to loop out a select, or a table, to write a lot of judgments.
Is there any better way?
My approach is to generate an array that can be called repeatedly, directly looping the array on the line.
For convenience, I wrote it as a class.

Class.asp

Returns an array of all categories, sorted in order
There are 4 properties:
Set Aa=new classlist
Aa.id= "id"//Number Name
Aa.classname= "classname"//category name
aa.pid= "pid"//Parent ID Name
Aa.db_name= "class"/table name
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 () ' Initialize variable
I=0
N=0
End Sub


Public Function Classarry (thisid,pid) ' gets subordinate 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 as not rs_c.eof

List (0,i) =rs_c (c_id) ' Mount in an 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 recursively called, until the last subclass

Rs_c.movenext
Loop
N=n-1
Rs_c.close

End Function

Public Function arrylist () ' loops out all root classes
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 array
Set Rs1=conn.execute ("select" &c_id& "from" &c_db_name& "where" &c_pid& "= 0")
Do as not rs1.eof
Call Classarry (Rs1 (c_id), 0)
' N=1
Rs1.movenext
Loop
Rs1.close
Arrylist=list
End Function

End Class
%>

Instance test:

Table class

Field
ID: Automatic Numbering
ClassName: Name
PID: Parent 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> name </td><td> category </ 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

Loop Result:

Www.hubro.net/code/class/test.asp

Basically can meet the general needs!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.