[thelist] More ASP/database (almost there)...

Scott Dexter sgd at ti3.com
Wed Apr 18 15:11:35 CDT 2001


> 
> > the best way is to let the database do all the work

For all of us learning or working with databases, this is our mantra.
ohm....

> > not sure how you handle the groups using ASP code, but in 
> cold fusion it
> > would look like this --

okay that's cool. almost enough to warrant me playing around with it =)

> 
> Does anyone have any idea how to do something like this with asp?

Welp, yer gonna have a recordset (think of it as a table, it helps me) that
looks like this:

Cat1	Subcat1
Cat1	Subcat2
Cat1	Subcat3
Cat2	Subcat1
Cat2	Subcat2
...

There are a couple ways to tackle this; 1) you can brute force it, by
looping through the recordset and keep track of the previous Category, and
if it changes, do your magic to separate the display. This would get the job
done in a single pass, but it's not in any way reusable elsewhere if you
have to do it a couple times (on the page, on the site), so I lean towards
2) store the recordset in an intermediary storage that allows you to work
with the data multiple times without going back to the recordset --a
dictionary.

Looping through the recordset, we're going to treat Category as the Key and
the SubCat(1,2,3...n) as a list of values stored under the Category (key).
In pseudocode (which is almost VBScript):

Dim dctCategories as Dictionary
Do while not Recordset.EOF
	CategoryKey = Recordset("Category")
	SubCat = Recordset("Subcategory")
	catlist = dctCategories(CategoryKey)
	dctCategories(CategoryKey).Value = catlist & SubCat & ","
	Recordset.MoveNext
Loop

blam. You've now got a dictionary with each category as the keys, and their
subcategories in a comma delimited list as the values. Note you will have to
worry about trailing comma at some point (depending on how you're going to
display the subcats), but we can kill them like so:

For each Key in dctCategories
	dctCategories(Key).Value = Left(dctCategories(Key).value,
Len(dctCategories(Key)) - 1)
next

--Or you can choose to deal with them at display time, which would be faster
than taking the extra time looping through the dictionary.

To display, roll through the dictionary by the keys and parse/display the
subcategory list as you wish (check that comma elimination loop above for a
hint on how to do that).

That help?
sgd
--
thinksafely.org




More information about the thelist mailing list