facebook  linkedin  Twitter  skype  Rss googlePlus

Recursive Sub-Procedures

Dec 20 1999

One of the best algorithms to know as a Web developer is how to code recursive sub procedures. What's recursion, and how does it help you as an ASP developer? Recursion allows you to code parent-child relationships. Parent-child relationships are the essence of directory tree-like structures. Trees allow you to develop threaded discussions, directory search engines (like santry.com), and perform lookups through your directories.

This process is done by calling a subroutine unto itself. What happens is the subroutine is initially called and then the subroutine calls itself until an end or condition is reached. Say for instance your doing a directory listing to list the contents of a directory, then each time the procedure encounters a new directory it calls itself to list the contents of the subdirectory, then the subroutine calls itself again to list the contents of directories in this sub directory and so on.

You can create this parent-child relationship in a database table. For example, the following table structure in a database:

ParentID DisplayName
1 0 Topic 1
2 0 Topic 2
3 1 RE: Topic 1
4 1 RE: Topic 1
5 2 RE: RE: Topic 1
6 2 RE: RE: Topic 1

You can see from the above table that we have several records, some of the records have a 0 as a ParentID, meaning this is a top level parent record, then other records do have a value other than 0 in the ParentID, meaning they are children of the record that has a matching ID in the table. This structure is very versatile, in that you can have unlimited child records using this structure, thus allowing you to create as many nests or branches you wish. You should now see why this algorithm is very useful when it comes to the Web. This structure drives those threaded forums, directories and all the cool apps that you want to be able to create.

The next piece of code shows how to make the SQL call and then call the subroutine in order to display this structure to the user.

Set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open "DSN=MyDSN"
'here we initially call the sub routine, we pass 0 as the parent ID
'this will pull all top level parent (meaning they don't have an 'ancestor).
'we also pass 0 for the level, this is used for spacing, or
'making the results appear threaded.
Sub DoTree(ParentID, intLevel)
Dim SQLQ, DBConn, rs, i
SQLQ = "SELECT RecordID, DisplayName FROM RECORDS " & _
      "WHERE ParentID = " & ParentID
       Set rs = DBConn.Execute(SQLQ)
       If Not rs.EOF Then
           Do Until rs.EOF
                 Response.Write ""
                 Response.Write rs("DisplayName") & "
'now call the subroutine we're in to see if this value has
'any children and increase the indent, and so on...    
                DoTree rs("RecordID"), intLevel + 1
       End If
       Set rs = Nothing
End Sub
Set DBConn= Nothing
'Once this routine is execute you should see results similiar to this:

Topic 1
RE: Topic 1
RE: RE: Topic 1
RE: RE: Topic 1
RE: Topic 1
Topic 2

So you can see from this example how you can use this to create a threaded type forum. You'll need to play around with the preceding code a bit and find out how you can put it to use in your application.

About the Author, Patrick Santry

Patrick Santry, has two decades of experience in enabling businesses to take advantage of the digital landscape. A well rounded experience in technology, and business is what sets me apart from the rest of the pack. When it comes to an overall digital strategy my experience is impressive.

BS in Computer Information Systems. Four time recipient of the Microsoft MVP Award, and author of several books and magazine articles on digital technologies.

blog comments powered by Disqus


Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.

Popular Articles