DisplayCategories Function

# Thread: DisplayCategories Function

1. Senior Member
Join Date
Dec 1969
Posts
156

## DisplayCategories Function

anyone wanna help me dissect this function from the article a few days ago so that I can better understand it?<BR><BR>Function DisplayCategories(aCategories, ByVal iCurID, ByVal iDepth)<BR> Dim strHTML<BR> strHTML = ""<BR> <BR> Const CategoryID = 0, Name = 1, Link = 2, ParentID = 3<BR> <BR> Dim iNumRecords, iLoop, i<BR> iNumRecords = UBound(aCategories, 2)<BR> For iLoop = 0 to iNumRecords<BR> If CInt(aCategories(ParentID, iLoop)) = CInt(iCurID) then<BR> For i = 1 to iDepth<BR> strHTML = strHTML & "&nbsp;"<BR> Next<BR> strHTML = strHTML & "&#060;a href=""" & aCategories(Link, iLoop) & """&#062;" & _<BR> aCategories(Name, iLoop) & "&#060;/a&#062;&#060;br /&#062;"<BR><BR> strHTML = strHTML & _<BR> DisplayCategories(aCategories, aCategories(CategoryID,iLoop), iDepth+1)<BR> End If<BR> Next<BR><BR> DisplayCategories = strHTML <BR>End Function<BR><BR>Here&#039;s my attempt at a line by line interpretation...let me know if i&#039;m off base on anything...<BR><BR>&#039;declare&#039;s the function, but I don&#039;t know what ByVal does.<BR>Function DisplayCategories(aCategories, ByVal iCurID, ByVal iDepth)<BR>&#039;declares the strHTML variable<BR> Dim strHTML<BR>&#039;resets strHTML to nothing<BR> strHTML = ""<BR>&#039;i&#039;m not sure why we do this. anyone?<BR> Const CategoryID = 0, Name = 1, Link = 2, ParentID = 3<BR>&#039;define some other variables<BR> Dim iNumRecords, iLoop, i<BR>&#039;create the upper bound of a loop based on the number of catagories in our DB<BR> iNumRecords = UBound(aCategories, 2)<BR>&#039;start looping<BR> For iLoop = 0 to iNumRecords<BR>&#039;condition for indenting the tree<BR> If CInt(aCategories(ParentID, iLoop)) = CInt(iCurID) then<BR> For i = 1 to iDepth<BR> strHTML = strHTML & "&nbsp;"<BR> Next<BR>&#039;render the menu item with indentation<BR> strHTML = strHTML & "&#060;a href=""" & aCategories(Link, iLoop) & """&#062;" & _<BR> aCategories(Name, iLoop) & "&#060;/a&#062;&#060;br /&#062;"<BR>&#039;im a little lost here but i think we&#039;re displaying any sub-catagories<BR> strHTML = strHTML & _<BR> DisplayCategories(aCategories, aCategories(CategoryID,iLoop), iDepth+1)<BR> End If<BR>&#039;loop until we are at ubound<BR> Next<BR>&#039;output of the function<BR> DisplayCategories = strHTML <BR>&#039;we&#039;re done<BR>End Function<BR><BR>Am I close?<BR>Thanks!

2. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## This help?

Function DisplayCategories(aCategories, ByVal iCurID, ByVal iDepth) &#039;declare the function<BR> Dim strHTML &#039;create a variable to hold the HTML<BR> strHTML = "" &#039;initialize the variable<BR> <BR> Const CategoryID = 0, Name = 1, Link = 2, ParentID = 3 &#039;create constants for easy reference to the array, cleans up code and makes changes easier<BR> <BR> Dim iNumRecords, iLoop, i &#039;declare variables to hold counters<BR> iNumRecords = UBound(aCategories, 2) &#039;get the # of categories from the DB<BR> For iLoop = 0 to iNumRecords &#039;loop through the categories<BR> If CInt(aCategories(ParentID, iLoop)) = CInt(iCurID) then &#039;if the parent matches the current ID, we want to do something<BR> For i = 1 to iDepth &#039;Go into a loop that runs for 1 to the depth<BR> strHTML = strHTML & " " &#039;Add a space to the HTML<BR> Next <BR> strHTML = strHTML & "&#060;a href=""" & aCategories(Link, iLoop) & """&#062;" & _ <BR> aCategories(Name, iLoop) & "&#060;/a&#062;&#060;br /&#062;" &#039;Add a link to the category<BR><BR> strHTML = strHTML & _ <BR> DisplayCategories(aCategories, aCategories(CategoryID,iLoop), iDepth+1) &#039;recursively call the function, passing the category array, the current category ID, and the depth, incremented by 1<BR> End If <BR> Next <BR><BR> DisplayCategories = strHTML &#039;give the function a value<BR>End Function

3. Senior Member
Join Date
Dec 1969
Posts
156

## sure does!

thanks!<BR><BR>You&#039;re a GOD...or maybe...*the* God???

4. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## However you want to..

.. slice and dice it.<BR><BR>Recursion is pretty cool - but can take some getting used to.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•