Sections
     
     
WWWCoder.com Resource Directory

Recursive Sub-Procedures
12/20/1999 12:00:00 AM

This tutorial explains how to do recursive subroutines in ASP (VBScript). Use this algorithm to create threaded discussions, directories, or whatever use you have for it.

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:

































RecordID
(autonumber)
ParentIDDisplayName
10Topic 1
20Topic 2
31RE: Topic 1
41RE: Topic 1
52RE: RE: Topic 1
62RE: 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.

DoTree(0,0)
'----------------------------------------------------------
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 "<img src=Spacer.gif Width= " & _
                 15 * intLevel & ">"
                 Response.Write rs("DisplayName") & "<br>"
'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 
               rs.MoveNext
           Loop
       End If
       rs.Close
       Set rs = Nothing 
End Sub

'------------------------------------------------------------

DBConn.Close
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 preceeding code a bit and find out how you can put it to use in your application.


Related Articles
   Related Document Recursive SQL User Defined Functions


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 3/14/2007 4:46:13 AM by Anonymous
Comments: I really appreciate that funtastic coding.
Left on 5/10/2006 7:04:18 AM by Anonymous
Comments: Recursive SQL queries I agree with you. But as far as this code is concerned, recursion is fine as long as you're using something that is stored in memory. This code was written in 1999 before you could do recursive functions in SQL.
No ratings available.
Left on 5/10/2006 2:27:04 AM by Anonymous
Comments: You should *NEVER* make recursive SQL queries like that!  Recursion, yes.  Recursive queries, never.  Scary that some people actually thought this was a good code example!
No ratings available.
Left on 11/1/2005 1:08:23 PM by Anonymous
Comments: Good routine for displaying the relationships.  Check out http://www.sitepoint.com/article/hierarchical-data-database/2 for creating and querying the table to store the relationships.
Left on 7/13/2005 10:08:49 PM by Anonymous
Comments: If a timestamp column was added to the table, how could the parent records be ordered in such a way that newest records appeared at the top?
No ratings available.
Left on 9/15/2004 3:27:58 PM by Anonymous
Comments: what if a child can be part of more than one parent? is there any examples for this?
No ratings available.
Left on 9/12/2004 1:49:06 AM by Anonymous
Comments: Spoint on that mate, thanks a lot
Left on 8/26/2004 1:38:01 PM by Anonymous
Comments: A memory hog of a routine
Left on 7/20/2004 7:01:51 PM by Anonymous
Comments: Parent-Child relationship table at the top is incorrect compared to the Example, Please correct it. URL as follows : http://www.wwwcoder.com/main/parentid/154/site/899/68/default.aspx

Thanks
Rajappa V

     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     
     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use