php把無限級分類產生數組的類

來源:互聯網
上載者:User
  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 "
  21. for j=0 to ubound(list,2)
  22. next
  23. < td>第幾類 "
  24. response.write "
  25. "
  26. response.write "
  27. ID 名稱
    "&list(0,j)&" "&list(1,j)&" "&list(2,j)&"
    "
  28. 'response.write list(1,3)
  29. %>
  30. <% for i=0 to ubound(list,2)%><%response.write ins(list(2,i))response.write list(1,i)%><%next%>
複製代碼

迴圈結果:bbs.it-home.org/code/class/test.asp基本可以滿足通常的需要啦。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.