• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

web app

to make a long story short
i have been asked if i can build a shopping cart where the products have a hierarchy that is virtually unlimited.
to explain a bit
normally I'd do Categories table > SubCategories table > Items Table
even if I used 1 table this is still the concept I'd use
What they want is to be able to have some items have cats > subcats > items
others might be cats > subcats > subsubcats > items
other items might be cats > subcats > subsubcats > subsubsubcats > items
for example

I'd like some suggestions on the concept to use for this
0
dgrafx
Asked:
dgrafx
  • 9
  • 8
1 Solution
 
chapmandewCommented:
I would imagine that you'd have some sort of parent/child relationship in the table....

CategoryID
ParentCategoryID

It would be a self referencing table
0
 
dgrafxAuthor Commented:
can you expand a bit on what a table might look like that has the ability to have an "unlimited" hierarchy?
and what would some basic sql look like that asks what subcat or subsubcat or whatever on a product page?

thanks ...
0
 
chapmandewCommented:
Sure...so, every category (record) in the table either belongs to the base category (parent), or is related to the parent...for example:

create table categories
(
categoryid int not null primary key,
parentcategoryid int null,
categoryname varchar(100)
)
go

insert into categories values (1, null, 'parent')
insert into categories values (2, 1, 'child1')
insert into categories values (3, 2, 'grandchild1')
insert into categories values (4, 1, 'child2')

in this case, the first category is the parent, and the others are related to the parent either by being a child or a grandchild, etc.  Does that make sense?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dgrafxAuthor Commented:
so in querying
if parentcategoryid is null then this is a top level category
if parentcategoryid is 1 then it is next level down (from null) subcat
if parentcategoryid is 2 then it is next level down (from 1) subcat

question
what would the table look like if
you have
categoryname of "tester" with catid of 1
categoryname of "testing" with catid of 2
these are both top level categories
tester has a "subcat" named "blue" and a subsubcat named "green"
testing has a subcat named "red"

just to help me get the concept

0
 
chapmandewCommented:
you want to see the data, right?

insert into categories values (1, null, 'tester')
insert into categories values (2, null, 'testing')
insert into categories values (3, 1, 'blue')
insert into categories values (4, 1, 'green')
insert into categories values (5, 2, 'red')
0
 
dgrafxAuthor Commented:
ok - great - so far so good
and to create a subsubcat under blue named yellow
insert into categories values (6, 3, 'yellow')

so then this particular hierarchy would be tester > blue > yellow
is that correct?

what sql would one use for a display page?
I'm guessing you have some slick method for saying is this a toplevel category or a child or a grandchild
so you know what type of "Stuff" to display
0
 
chapmandewCommented:
You are, in fact, using 2005, right?
0
 
dgrafxAuthor Commented:
yes - but i believe the live app will run on 2000
if there is a difference - please post both as this is the first 2000 app i've done in a bit and may not end up doing another
0
 
chapmandewCommented:
Well, it is a LOT easier in 2005 to write this as it supports recursive queries.  2000 really doesn't do a good job of it.
0
 
dgrafxAuthor Commented:
i imagine so
unfortunately the decision is out of my hands ...
0
 
chapmandewCommented:
OK.

So, what kind of SQL results are you looking to see?
0
 
dgrafxAuthor Commented:
i guess in general something like
now on a page where one is expecting to see categories
i query for categories and display catname - description (if any) - image (if any) etc
then when that is clicked I go to another page that queries for subcats where the same type of thing is done and when clicked I go to an items page
so I use 3 different display pages
but it occurs to me that with this new method one may still have an items page that has the typical "add to cart" functionality but the cats & subcats & subsubcats etc would probably be on the same page so am just looking for some pointers as to how do i tell at what level is the data at that I'm currently looking at?

thanks ...
0
 
chapmandewCommented:
OK...this will get you the first level (parent) and any direct child levels...


create table categories
(
categoryid int not null primary key,
parentcategoryid int null,
categoryname varchar(100)
)
go
 
insert into categories values (1, null, 'tester')
insert into categories values (2, null, 'testing')
insert into categories values (3, 1, 'blue')
insert into categories values (4, 1, 'green')
insert into categories values (5, 2, 'red')
 
create function udf_recurse
(
@catid int, 
@parentid int = null
)
returns @test table(
categoryid int ,
parentcategoryid int ,
categoryname varchar(100))
as
begin
insert into @test
select * from categories where categoryid = @catid
union
select * from categories where parentcategoryid = @catid
 
return
end
 
 
select * from udf_recurse(1,1)

Open in new window

0
 
dgrafxAuthor Commented:
ok
is this the 2000 method?

do you have a 2005 method for my own future use?

thanks ...
0
 
chapmandewCommented:
sure...one second.  You're going to have the tweak the 2000 version a bit for your own purposes....
0
 
chapmandewCommented:
with catrecurse(catid, parentcatid, catname, rowno, levelrow)
as
(
select categoryid, parentcategoryid, categoryname,        ROW_NUMBER() OVER(ORDER BY categoryid),
       0 AS LevelRow
from categories
where parentcategoryid is null
union all
select c.categoryid, c.parentcategoryid, c.categoryname, r.rowno, r.levelrow + 1
from categories c
join catrecurse r on c.parentcategoryid = r.catid
where parentcategoryid is not null
)

select * from catrecurse
0
 
dgrafxAuthor Commented:
pretty cool ...
thank you very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now