当前位置:Gxlcms > PHP教程 > php把无限级分类生成数组的类

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

时间:2021-07-01 10:21:17 帮助过:16人阅读

  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. <%
  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()'初始化变量
  22. i=0
  23. n=0
  24. End Sub
  25. public function classarry(thisid,pid)'取得下级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)'装入数组中
  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)'这里递归调用,直到最后一个子类
  40. rs_c.movenext
  41. loop
  42. n=n-1
  43. rs_c.close
  44. end function
  45. public function arrylist()'循环出所有根类
  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)'设置数组
  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. %>

实例测试: 表class 字段 id:自动编号 classname:名称 pid:父ID 文件名: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>第几类"
  21. for j=0 to ubound(list,2)
  22. response.write "
  23. "
  24. next
  25. response.write "
  26. ID名称
    "&list(0,j)&""&list(1,j)&""&list(2,j)&"
    "
  27. 'response.write list(1,3)
  28. %>

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

人气教程排行