Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating nested <ul> tree structure from ParentID's in a db

This seems like a pretty common task I want to do, but I can't wrap my head around the cfloops and cfqueries.

I have a database full of photo galleries. They all have an ID and a ParentID (except the root galleries - their ParentID is blank), and they can go multiple levels deep. Here's an example of the gallery structure:

  • 1
    • 1-1
    • 1-2
  • 2
    • 2-1
      • 2-1-1
      • 2-1-2
    • 2-2

I want to automatically output this structure above into nested ul's and li's (just like above). How is that done? I can't wrap my head around the placement of the ul's and li's to display the hierarchy levels correctly.

like image 441
Michael Avatar asked Apr 14 '26 15:04

Michael


1 Answers

I guess the simpliest solution would be to use Query of Query...

If the dataset is huge, try <cftree> and populate the leaves on demand.

like image 76
Henry Avatar answered Apr 16 '26 07:04

Henry