PHP class that generates an array of infinite class classification

Source: Internet
Author: User
Tags php class
    1. Set Aa=new classlist
    2. Aa.id= name of "id"//number
    3. Aa.classname= "classname"//category name
    4. aa.pid= "pid"//Parent ID Name
    5. Aa.db_name= "class"//Table name
    6. List=aa.arrylist ()
    7. ?>
Copy Code

Class: Classlist

  1. <%
  2. Class Classlist
  3. Private c_id
  4. Private C_db_name
  5. Private C_pid
  6. Private C_classname
  7. Public Property Let ID (str)
  8. c_id = str
  9. End Property
  10. Public Property Let Db_name (str)
  11. C_db_name = str
  12. End Property
  13. Public Property Let PID (str)
  14. C_pid = str
  15. End Property
  16. Public Property Let classname (str)
  17. C_classname = str
  18. End Property
  19. Dim list ()
  20. Dim i,n
  21. Private Sub class_initialize () ' Initialize variable
  22. I=0
  23. N=0
  24. End Sub
  25. Public Function Classarry (thisid,pid) ' Get subordinate ID
  26. If Pid>0 Then
  27. Sql= "SELECT * from" &c_db_name& "where" &c_pid& "=" &thisid
  28. Else
  29. Sql= "SELECT * from" &c_db_name& "where" &c_id& "=" &thisid
  30. End If
  31. Set Rs_c=conn.execute (SQL)
  32. N=n+1
  33. Do and not rs_c.eof
  34. List (0,i) =rs_c (c_id) ' loaded into the array
  35. List (1,i) =rs_c (c_classname)
  36. List (2,i) =n
  37. ' N=n+1
  38. I=i+1
  39. Thisid=classarry (Rs_c (c_id), 1) ' here recursively calls until the last subclass
  40. Rs_c.movenext
  41. Loop
  42. N=n-1
  43. Rs_c.close
  44. End Function
  45. Public Function arrylist () ' loops out all root classes
  46. Set Rs_c=conn.execute ("SELECT count (" &c_id& ") from" &c_db_name ")
  47. Lenght=rs_c (0)
  48. Rs_c.close
  49. ReDim List (2,lenght) ' Set array
  50. Set Rs1=conn.execute ("select" &c_id& "from" &c_db_name& "where" &c_pid& "= 0")
  51. Do and not rs1.eof
  52. Call Classarry (Rs1 (c_id), 0)
  53. ' N=1
  54. Rs1.movenext
  55. Loop
  56. Rs1.close
  57. Arrylist=list
  58. End Function
  59. End Class
  60. %>
Copy Code

Instance test: Table class Field ID: AutoNumber classname: Name PID: Parent ID file name: test.asp

  1. <%
  2. Set conn=server.createobject ("Adodb.connection")
  3. Set Rs = Server.CreateObject ("ADODB". Recordset ")
  4. strDSN = "Driver={microsoft Access Driver (*.mdb)}; Dbq= "
  5. strDSN = strDSN & Server.MapPath ("Test.mdb")
  6. Conn. Open strDSN
  7. function ins (num)
  8. Str= ""
  9. For Ii=1 to Num
  10. str=str& "|"
  11. Next
  12. Ins=str
  13. End Function
  14. Set Aa=new classlist
  15. Aa.id= "id"
  16. Aa.classname= "ClassName"
  17. Aa.pid= "pid"
  18. Aa.db_name= "Class"
  19. List=aa.arrylist ()
  20. Response.Write "
  21. For j=0 to UBound (list,2)
  22. Next
  23. Response.Write "
  24. Response.Write "
  25. < td> categories " "
    Id Name
    "&list (0,j) &" "&list (1,j) &" "&list (2,j) &"
    "
  26. ' Response.Write List (1,3)
  27. %>
  28. <% for I=0 to UBound (list,2)%><%response.write ins (list (2,i)) Response.Write List (1,i)%><%next%>
Copy Code

Loop Result: bbs.it-home.org/code/class/test.asp Basically can meet the usual needs.

  • 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.