Category/Subcategory Display Demo

This demo illustrates the algorithm discussed in: Efficiently Displaying Parent-Child Data. Note that for this demo the array passed into the DisplayCategories function is not generated by GetRows(); rather, it is created by hand, but it is created so that it has the same structure that an array created by GetRows() would. For more information on GetRows() be sure to read the FAQ: How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?


Payroll
     Submit Your Hours
HR Info
     Benefits
          Insurance
          401(k) Info
     Latest News
Press Clippings
     Stock Quotes


Source Code
<%
Function DisplayCategories(aCategories, ByVal iCurID, ByVal iDepth)
  Dim strHTML
  strHTML = ""
  
  Const CategoryID = 0, Name = 1, Link = 2, ParentID = 3
  
  Dim iNumRecords, iLoop, i
  iNumRecords = UBound(aCategories, 2)
  For iLoop = 0 to iNumRecords
    If CInt(aCategories(ParentID, iLoop)) = CInt(iCurID) then
      For i = 1 to iDepth
        strHTML = strHTML & "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
      Next
      strHTML = strHTML & "<a href=""" & aCategories(Link, iLoop) & """>" & _
                aCategories(Name, iLoop) & "</a><br />"

      strHTML = strHTML & _
        DisplayCategories(aCategories, aCategories(CategoryID,iLoop), iDepth+1)
    End If
  Next

  DisplayCategories = strHTML  
End Function


  Dim aMyCategories()
  Redim aMyCategories(3,8)
  
  'Payroll category
  aMyCategories(0,0) = 1
  aMyCategories(1,0) = "Payroll"
  aMyCategories(2,0) = "http://www.somesite.com/Payroll"
  aMyCategories(3,0) = 0

  'HR Info category
  aMyCategories(0,1) = 2
  aMyCategories(1,1) = "HR Info"
  aMyCategories(2,1) = "http://www.somesite.com/HR"
  aMyCategories(3,1) = 0

  'Submit Your Hours category
  aMyCategories(0,2) = 3
  aMyCategories(1,2) = "Submit Your Hours"
  aMyCategories(2,2) = "http://www.somesite.com/Payroll/syh"
  aMyCategories(3,2) = 1

  'Press Clippings category
  aMyCategories(0,3) = 4
  aMyCategories(1,3) = "Press Clippings"
  aMyCategories(2,3) = "http://www.somesite.com/press"
  aMyCategories(3,3) = 0

  'Stock Quotes category
  aMyCategories(0,4) = 5
  aMyCategories(1,4) = "Stock Quotes"
  aMyCategories(2,4) = "http://www.somesite.com/press/quote/"
  aMyCategories(3,4) = 4

  'Benefits category
  aMyCategories(0,5) = 6
  aMyCategories(1,5) = "Benefits"
  aMyCategories(2,5) = "http://www.somesite.com/HR/Benefits"
  aMyCategories(3,5) = 2

  'Latest News category
  aMyCategories(0,6) = 7
  aMyCategories(1,6) = "Latest News"
  aMyCategories(2,6) = "http://www.somesite.com/HR/LatestNews"
  aMyCategories(3,6) = 2

  'Insurance category
  aMyCategories(0,7) = 8
  aMyCategories(1,7) = "Insurance"
  aMyCategories(2,7) = "http://www.somesite.com/HR/Benefits/Insurance"
  aMyCategories(3,7) = 6

  '401(k) Info category
  aMyCategories(0,8) = 9
  aMyCategories(1,8) = "401(k) Info"
  aMyCategories(2,8) = "http://www.somesite.com/HR/Benefits/401k"
  aMyCategories(3,8) = 6


  Response.Write(DisplayCategories(aMyCategories,0,0))
%>


[Return to the article.]