Skip to main content
Participant
October 4, 2006
Question

Creating a TOC - Need some help looping through table

  • October 4, 2006
  • 3 replies
  • 362 views
I have a database that consists of a table with a SectionID (primary key), a ParentID, and a SectionTitle. For example, see below (sorry if my makeshift table looks funny).

Anyways, I am looking for some suggestions and ideas on how to generate (most efficiently) a table of contents based on this information... similar to the sample TOC below my example table. What would be the best method to output the TOC with a seemingly unlimited amount of sub-sections? Right now, I currently have four seperate loops to get up to four subsections, I know it's a bad way to do it.. and that's why I am here =). So, if more subsections were required in the database... I'd like the loop to take into account each of them.. whether there are 2 subsections or 20.. whatever the case may be.

Any help, ideas, or suggestions would be great. Code examples would be fantastic. I think I was clear enough, but it has been a very long day... so, if anyone needs clarification, don't hesitate to ask.

Thanks a lot!
Cody Rush


This topic has been closed for replies.

3 replies

crush78Author
Participant
October 9, 2006
wow thanks! great info!!

cody
Inspiring
October 9, 2006
What I've done in the past.

Query the database sorting the record set by Parent_ID.

Loop over the record set for records top level records

In each iteration sub loop over any record that has the current record
ID for a parent ID. QofQs and|or Custom Tags are very useful for this.

End Loop.
crush78Author
Participant
October 9, 2006
Any ideas? Suggestions?

Thx,
Cody
October 9, 2006
The classic solution is to use a recursive stored procedure.
The smart solution is to convert the table to a nested set model.
Read Joe Celko!