Home >Backend Development >PHP Tutorial >PHP classifies infinite levels into arrays

PHP classifies infinite levels into arrays

WBOY
WBOYOriginal
2016-07-25 09:03:291029browse
  1. set aa=new classlist
  2. aa.id="id"//Name of the number
  3. aa.classname="classname"//Classification 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 the 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 while not rs_c.eof
  34. list(0, i)=rs_c(c_id)' is 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)' is called recursively here 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 while 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

Example test: Table class Field id: automatic number 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 "< td>Category"
  21. for j=0 to ubound(list,2)
  22. response.write ""&list(0,j)&"
  23. "
  24. next
  25. response.write "
  26. IDName
    "&list(1,j)&""&list(2,j)&"
    "
  27. 'response.write list(1,3)
  28. %>
Copy code

Loop results: bbs.it-home.org/code/class/test.asp It can basically meet the usual needs.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn