Ohh quick addendum, as fun as this stuff is... I'm a big fan of letting
someone else do the work.

OSs are great at this, they provide you with a nice organised tree structure
and symbolic links should you wish to cross reference categories.

Reading dirs is A LOT easier for finding parents too and much less processor
intensive.

-----Original Message-----
From: Raymond B. [mailto:[EMAIL PROTECTED]]
Sent: May 17, 2001 05:11
To: CF-Talk
Subject: RE: HELP: Hierarchy


Sounds like you'd be best be served by a recursive function (or for CF
recursive custom tag).

For performance sake I'd either have a table or application object to store
the full heirchy info of each category; pre-generated on a fixed basis.

You could store it w/ full html formating in a table/array:

| cat_id   | generated_data
-------------------------------------------
| 5        | <a href="blah... >> <a href=

Or if you wanted a more flexible approach an array of struct containing the
cat_id and cat_name (wddxed into that field if a db or flatfile).

array[0]->{24} = 'Sevier County'
array[1]->{6} = 'Arkansas'
array[i]->{cat_id} = cat_name

(Alternately a 2D array would accomplish the same thing w/ better
performance but this is for clarity)

I'm using a Perlish notation to show the relation a bit more clearly (I
hope).

Whether you use a temporary table, memory object, or an external file really
depends on your hardware and extent of the data.


Okay, so how exactly do you implement recurision in CF you ask? Well I'm
glad you did, let's go over that.

The first thing you always want to think of w/ recursion is "How the hell am
I going to stop it?" or you'll introduce all sorts of fun infinite loops
your sysadmin will just love. In this case our stop criteria is when
parent.recordcount in fig. 3 (heh, I feel so official mentioning figure
numbers) becomes 0.

// fig. 3

<cfquery name="parent">
SELECT cat_id, cat_name
FROM table
WHERE paren_id = attributes.cat_id
</cfquery>


Okay, well that defines our stop position and at the same time shows us a
large part of our custom tag. Now let's start our thinking at the root
parent (the absolute last interation of our custom tag) and work backward.
Well we know we want to track the heirarchy, so we need to pass that parent
value to the calling tag.

caller.parent_cat = parent.cat_id;
caller.parent_name = parent.cat_name;

Okay, so that's done.. we're now down one level and we need to pass both the
root parent, and this level is a parent too. Oviously passing tons of
scalars isn't going to go over well w/ untold levels of recursion, so
instead let's move to the more formatted data structure stated before we
began.

So we have an id and name for each level of recursion. Let's save that as a
simple array:

current = listToArray(cat_id, cat_name)

That handles the last level (remeber root parent is the last tag to execute)
but we need to track as we go, so let's store these arrays in another so we
can pass them along and also keep track of the order (which is the whole
purpose). So we'll arrayAppend this (push, pop, etc. are named differently
in CF) array into our tracking one and pass it along to the previous caller.

arrayAppend(tracking, current);
caller.parents = tracking;


**[Okay. it's 5:04am and as fun as the last 10-15min writing this has
been... I have a bad cold and need my sleep. Someone else can probably
finish this off (or suggest a better solution overall). If not I'll continue
where I left off tomorrow sometime and it will probably be a lot more lucid
for it.

2 quick pointers:
1) you have to write a page to loop over every line in a select * type query
to start up the recursion.
2) that page will also deal w/ how you plan to store the info (key being
cat_id, data being the complete lineage of each cat_id

This is the extreme case of normalisation vs. perfomance :)
]**


Happy coding.

-----Original Message-----
From: Tony Hicks [mailto:[EMAIL PROTECTED]]
Sent: May 17, 2001 03:44
To: CF-Talk
Subject: HELP: Hierarchy


I have passed this question through so many cf communities... and it has
still left me

<cf_dumbfounded>

Suppose you have a db with a table called categories..

The fields are:

ID: Each Category's Unique ID
Parent_ID: This field is the ID of the Parent Category
Category_Name: The text to display to represent the category...

So when you click a link like this: <a href="browse.cfm?parent_id=5>Test
Category</a> (The unprocessed CF for that is <a
href="browse.cfm?parent_id=#id#>#Category_Name#</a> with a query that tells
it to select just from the parent_id..)...

Well that all works fine but I want to display the Level history at the top
like so:

Companies >> Brick and Mortar >> United States >> Arkansas >> Sevier County

And if they were on the page listing all the counties in Arkansas it would
be:

Companies >> Brick and Mortar >> United States >> Arkansas

Hopefully you all get the idea... does anyone know what the proper cf code
would be to display this? Allaire Forums have helped me the best they can
but the code people give me either errors out or loops into itself, thus
locking up the server...

Thanks,
Tony Hicks

I'd be glad to provide as much information as needed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to