• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Creating a TOC - Need some help looping through table

New Here ,
Oct 03, 2006 Oct 03, 2006

Copy link to clipboard

Copied

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


TOPICS
Advanced techniques

Views

299

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2006 Oct 09, 2006

Copy link to clipboard

Copied

Any ideas? Suggestions?

Thx,
Cody

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 09, 2006 Oct 09, 2006

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2006 Oct 09, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2006 Oct 09, 2006

Copy link to clipboard

Copied

LATEST
wow thanks! great info!!

cody

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation