欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php把无限级分类生成数组的类

程序员文章站 2022-05-21 15:38:59
...
  1. set aa=new classlist
  2. aa.id="id"//编号的名称
  3. aa.classname="classname"//分类名称
  4. aa.pid="pid"//父ID名称
  5. aa.db_name="class"//表名
  6. list=aa.arrylist()
  7. ?>
复制代码

类:classlist

  1. class classlist
  2. private c_id
  3. private c_db_name
  4. private c_pid
  5. private c_classname
  6. public property let id(str)
  7. c_id = str
  8. end property
  9. public property let db_name(str)
  10. c_db_name = str
  11. end property
  12. public property let pid(str)
  13. c_pid = str
  14. end property
  15. public property let classname(str)
  16. c_classname = str
  17. end property
  18. dim list()
  19. dim i,n
  20. Private Sub Class_Initialize()'初始化变量
  21. i=0
  22. n=0
  23. End Sub
  24. public function classarry(thisid,pid)'取得下级ID
  25. if pid>0 then
  26. sql="select * from "&c_db_name&" where "&c_pid&"="&thisid
  27. else
  28. sql="select * from "&c_db_name&" where "&c_id&"="&thisid
  29. end if
  30. set rs_c=conn.execute(sql)
  31. n=n+1
  32. do while not rs_c.eof
  33. list(0,i)=rs_c(c_id)'装入数组中
  34. list(1,i)=rs_c(c_classname)
  35. list(2,i)=n
  36. 'n=n+1
  37. i=i+1
  38. thisid=classarry(rs_c(c_id),1)'这里递归调用,直到最后一个子类
  39. rs_c.movenext
  40. loop
  41. n=n-1
  42. rs_c.close
  43. end function
  44. public function arrylist()'循环出所有根类
  45. set rs_c=conn.execute("select count("&c_id&") from "&c_db_name)
  46. lenght=rs_c(0)
  47. rs_c.close
  48. redim list(2,lenght)'设置数组
  49. set rs1=conn.execute("select "&c_id&" from "&c_db_name&" where "&c_pid&"=0")
  50. do while not rs1.eof
  51. call classarry(rs1(c_id),0)
  52. 'n=1
  53. rs1.movenext
  54. loop
  55. rs1.close
  56. arrylist=list
  57. end function
  58. end class
  59. %>
复制代码

实例测试: 表class 字段 id:自动编号 classname:名称 pid:父ID 文件名:test.asp

  1. Set conn=Server.CreateObject("ADODB.connection")
  2. Set Rs = Server.CreateObject("ADODB.Recordset")
  3. StrDSN = "Driver={Microsoft Access Driver (*.mdb)}; DBQ="
  4. StrDSN = StrDSN & Server.MapPath("test.mdb")
  5. conn.Open strDSN
  6. function ins(num)
  7. str=""
  8. for ii=1 to num
  9. str=str&"|-"
  10. next
  11. ins=str
  12. end function
  13. set aa=new classlist
  14. aa.id="id"
  15. aa.classname="classname"
  16. aa.pid="pid"
  17. aa.db_name="class"
  18. list=aa.arrylist()
  19. response.write "第几类"
  20. for j=0 to ubound(list,2)
  21. response.write "
  22. "
  23. next
  24. response.write "
  25. ID 名称
    "&list(0,j)&" "&list(1,j)&" "&list(2,j)&"
    "
  26. 'response.write list(1,3)
  27. %>
复制代码

循环结果: bbs.it-home.org/code/class/test.asp 基本可以满足通常的需要啦。