Writing a Parent-Child Query Sorter in CF, Part 1

One of the Stupid-Web-Programming-Tricks I end up recoding quite often is a sorter for queries with Parent-Child relationships. Trees, basically. You know the situation: some yahoo wants theoretically infinite levels of depth to his navigational menus or his product categories for his e-commerce system. What you end up with is a table in a database that looks like this:
ItemID Title ParentID
This should magically yield a list that looks like this:
  1. Coding
    1. Applications
      1. C++
    2. Hybrid
      1. Perl
    3. Web
      1. ColdFusion
  2. Food
    1. Cheese
Getting from Point A to Point B isn’t all that hard, especially when you have the time, foresight, and ability to plan ahead. There are dozens of different ways to accomplish this efficiently within the database. Most of the time, however, I find that I have read-only access to the database table and end up having to do the organization in code. In this series of posts I’ll be explaining my solution and how I get to it.

Published by

Rick Osborne

I am a web geek who has been doing this sort of thing entirely too long. I rant, I muse, I whine. That is, I am not at all atypical for my breed.