See below:
[quoted text, click to view] Dragonhunter wrote:
> "Bob Barrows [MVP]" wrote:
>
>> Can you provide a few rows of sample data for each table (in tabular
>> form) and the desired result? Descriptions can only go so far, plus
>> it will help us get to the correct solution if we are discussing the
>> data that you will be using.
>>
>> Bob Barrows
>
> Sure, here is my database and an example of the web page I want to
> create to output the data. I structured the database to normalize
> after being explained the concept on this group (thanks!). So, how
> do I do this recursive displaying of data properly using ASP? I
> think I'm pretty stuck on the SQL..., particularly when I try to do
> the "join" method instead of nested loops. Any help would be
> appreciated!
>
> Thanks,
> Dragonhunter
>
>
I would probably have added a parent_id column to this table. The
subcategories table is really not needed. The expand field is really not
needed either, although it will make the subsequent coding easier. I'm not
sure this coding ease is worth the headaches that guaranteeing this field
will contain the correct value will cause.
Like this
[quoted text, click to view] > category
> id--name--------------------parent_id
> 1 Favorite soundtracks 2
> 2 Favorite music
> 3 Pets
> 4 Favorite quotes
> 5 Ordered soundtracks
However, I will assume you do not make this change
[quoted text, click to view] >
> note
> id--text
> 1 Star Wars
> 2 Indiana Jones
> 3 Fluffy the cat
> 4 Goldfish 1
> 5 "To make an omelette you have to break some eggs"
> 6 "Nested loops are the root of all evil"
> 7 Batman
> 8 Predator
> 9 "it takes one to know one!"
> 10 Les miserables
>
> note_categories
> note_id--category_id
> 1 1
> 1 5 <--this is a note in two categories
> 2 1
> 3 3
> 4 3
> 5 4
> 6 4
> 7 1
> 8 1
> 9 4
> 10 1
> 7 5 <--this is a note in two categories
>
> Subcategories
> child_id--parent_id
> 1 2
>
>
> Webpage==============================================
> + is expandable to show things under it
> - means un-expand it (and store +/- state in a database)
>
>
> -Favorite music
> -Favorite soundtracks
> Star Wars
> Indiana Jones
> Batman
> Predator
> Les miserables
> +Pets <-- none show because "expand" field is false
> -Favorite quotes
> "To make an omelette you have to break some eggs"
> "Nested loops are the root of all evil"
> "it takes one to know one!"
> -Ordered soundtracks
> Star Wars
> Batman
OK. I will take care of showing you how to do the nesting. You will need to
take care of the client-side coding needed to expand and collapse the nodes.
You can ask about that in a client-side code newsgroup (newsgroups
containing "dhtml" in their title or one of the .scripting newsgroups). I
will put the + and - characters in spans whose onclick events can be used to
expand or collapse a node.
To start, let's order the categories by parent_id, then child id, so the
master categories will be first:
SELECT c.id, iif(sc.parent_id is null,0,sc.parent_id), c.name, c.expand
FROM category AS c LEFT JOIN subcategories AS sc ON c.id = sc.child_id
ORDER BY sc.parent_id, c.id;
produces this resultset:
id parent_id name expand
2 0 Favorite music -1
3 0 Pets 0
4 0 Favorite quotes -1
5 0 Ordered soundtracks -1
1 2 Favorite soundtracks -1
Now let's get the notes:
SELECT n.id, c.id, n.text, null AS Expr1
FROM (category AS c
INNER JOIN note_categories AS nc ON c.id = nc.category_id)
INNER JOIN [note] AS n ON nc.note_id = n.id
ORDER BY c.id, n.id;
which gives us this result:
n.id c.id text
Expr1
1 1 Star Wars
2 1 Indiana Jones
7 1 Batman
8 1 Predator
10 1 Les miserables
3 3 Fluffy the cat
4 3 Goldfish 1
5 4 "To make an omelette you have to break some eggs"
6 4 "Nested loops are the root of all evil"
9 4 "it takes one to know one!"
1 5 Star Wars
7 5 Batman
Let's union these results together:
SELECT c.id, iif(sc.parent_id is null,0,sc.parent_id) as parent_id,
c.name, c.expand, "cat" As Type
FROM category AS c LEFT JOIN subcategories AS sc ON c.id = sc.child_id
UNION ALL SELECT n.id, c.id, n.text, Null, "note"
FROM (category AS c
INNER JOIN note_categories AS nc ON c.id = nc.category_id)
INNER JOIN [note] AS n ON nc.note_id = n.id
ORDER BY parent_id, id;
giving these results:
qTree
id parent_id name
expand type
2 0 Favorite music
True cat
3 0 Pets
False cat
4 0 Favorite quotes
True cat
5 0 Ordered soundtracks
True cat
1 2 Favorite soundtracks
True cat
1 1 Star Wars
note
2 1 Indiana Jones
note
7 1 Batman
note
8 1 Predator
note
10 1 Les miserables
note
3 3 Fluffy the cat
note
4 3 Goldfish 1
note
5 4 "To make an omelette you have to break some eggs"
note
6 4 "Nested loops are the root of all evil"
note
9 4 "it takes one to know one!"
note
1 5 Star Wars
note
7 5 Batman
note
Copy the above sql into the SQL View window of an Access Query Builder and
save it as qTree after running it to verify the results are correct.
In your asp page, do this (as a bonus, I included the client-side code to
make the nodes expand and collapse):
<%
dim cn, rs, sSQL
set cn=createobject("adodb.connection")
cn.open "provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("db7.mdb")
set rs=CreateObject("adodb.recordset")
rs.CursorLocation = 3 'adUseClient
rs.LockType = 1 'adLockReadOnly
cn.qTree rs
'disconnect the recordset:
set rs.ActiveConnection=nothing
cn.Close: set cn = nothing
if rs.EOF then
Response.Write "no records returned"
else
'stop
WriteChildDivs rs.Clone,rs(1).Value,"cat",-10
rs.Close:set rs = nothing
end if
Sub WriteChildDivs(pRS,pID,byval pType,pIndent)
dim bNextLvlVisible
pRS.Filter="parent_id=" & pID & " and type='" & pType & "'"
if pRS.eof then
ptype = "note"
pRS.Filter="parent_id=" & pID & " and type='" & pType & "'"
end if
do until pRS.EOF
Response.Write "<div style=""margin-left:" & _
pIndent + 10 & "px"">"
select case pRS(3).value
case true
Response.Write "<span onclick=""handleclick();""" & _
" style=""cursor:hand"">"
Response.Write "-</span>"
bNextLvlVisible=true
case false