Solved

web app

Posted on 2008-06-11
17
167 Views
Last Modified: 2010-03-19
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
Comment
Question by:dgrafx
  • 9
  • 8
17 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759193
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
 
LVL 25

Author Comment

by:dgrafx
ID: 21759450
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
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21759485
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 25

Author Comment

by:dgrafx
ID: 21759696
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759770
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
 
LVL 25

Author Comment

by:dgrafx
ID: 21760056
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760079
You are, in fact, using 2005, right?
0
 
LVL 25

Author Comment

by:dgrafx
ID: 21760108
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760135
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
 
LVL 25

Author Comment

by:dgrafx
ID: 21760162
i imagine so
unfortunately the decision is out of my hands ...
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760185
OK.

So, what kind of SQL results are you looking to see?
0
 
LVL 25

Author Comment

by:dgrafx
ID: 21760286
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760386
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
 
LVL 25

Author Comment

by:dgrafx
ID: 21760486
ok
is this the 2000 method?

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

thanks ...
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760575
sure...one second.  You're going to have the tweak the 2000 version a bit for your own purposes....
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21760626
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
 
LVL 25

Author Comment

by:dgrafx
ID: 21760802
pretty cool ...
thank you very much
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now