SQL,FAST,unlimited,depth,tree,structure,witho
Quick Search for:  in language:    
SQL,FAST,unlimited,depth,tree,structure,witho
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 117,337 lines
 Jobs: 143 postings

 
Sponsored by:

 

You are in:

 
Login


 

 


Latest Code Ticker for ASP/ VbScript.
Click here to see a screenshot of this code!Ultimate Forum 2
By Gurgen Alaverdian on 6/1

(Screen Shot)

Speedy ASP Discussion Forum BBS
By ASPwebServer on 6/1


[[ Learn Database and ASP by Video ]]]
By Brian Link on 5/31


Generic XML Grabber
By Kevin O'Neill on 5/31


Block User to copy Links
By Shahid allauddin on 5/31


Click here to see a screenshot of this code!geographical search
By Igor Krupitsky on 5/31

(Screen Shot)

Click here to see a screenshot of this code!CDF/Active Desktop Example without ASP
By John on 5/30

(Screen Shot)

Click here to see a screenshot of this code!CDF/Active Desktop Example with ASP
By John on 5/30

(Screen Shot)

Date & Time Function
By Marc Ripin on 5/30


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Unlimited Depth Category Tree

Print
Email
 

Submitted on: 3/8/2002 1:29:14 PM
By: Michael V Feranda  
Level: Advanced
User Rating: By 30 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this article 4290 times.
 

(About the author)
 
     An unlimited depth tree structure without the use of special objects. Only done using a trick on the database and simple SQL. NO SPECIAL CODING!! Works FAST... Nothing more than an easy serial number.

This article has accompanying files

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Since there is confusion over the unlimited part, the unlimited comes with the depth... how many categories you can go in... Sure, using the parent cat method you could... but just try and manage it. My code shows you an idea to use a serial system for your category tree data. With this serial, you could take a category thats 100 steps within a tree and easily get all the categories mapped out to how they got there. Loop? nope, with a simple SQL statement, and thats why its so beautiful. Any fool knows that SQL is much faster than building lines upon lines of code to sort data.

This is VERY easy to do!!
If you download the zip file, you will find a example database and code.
OK... the best thing to do first is setup your database. Yes, we are dealing with an int. serial number, but we dont want the database to know it as a serial. Why? By the database understanding the serial as letters, it will put it in an ABC structure instead of 1,2,3,4. So just set your database to any text type, but not an int. type. Make the field a large amount of characters so you dont run into any size problems.
If you look at the picture attached, you can see the idea of the serial number structure. Lets say you want to put all the pages of a site into a tree structure... the first page or home page is first to show, so lets assign it a number.


Home - 001

Now, all pages under the home page will start out with 001, then they will get their own number as well. Lets say the about us/contact us/online store is under the home page... lets assign them a serial.

Home 001
  About Us 001001
  Contact Us 001002
  Online Store 001003


Now, the same goes the further you go into the category structure, you just take the above category serial and add another three digit number to it starting with 001... of course, you see that you are limited to 999 categories under one, but that should be enough!!! If not, try 4 numbers instead of three... or 5... (0001 or 00001) this method will still give you the same output, just increases the amout of categories you can have under a single category.
Lets add a few more nodes.

Home 001
  About Us 001001
    Company Profile 001001001
      Stock Market 001001001001
    Map to us 001001002
  Contact Us 001002
    Mailing Address 001002001
    Phone Information 001002002
  Online Store 001003
    Computer Related 001003001
      Hard Drives 001003001001
      Software 001003001002
      Motherboards 001003001003
    Furniture 001003002
      Computer Desks 001003002001
      Office Chairs 001003002002


Now, with each of those items in your database, if you did your SQL statement as:
SELECT * FROM table ORDER BY serial
It will output them all in ABC order... what do i mean by abc order... well... if the database understood these as numbers it would sort out as if the serial numbers had a numeric value... like:


001 = 1
001001 = 1,001
001001001 = 1,001,001

we dont want that, by it sorting in abc order... it looks at each character and sorts by greatest of that character. Example:

001
001001
001001001
001001001001
001001002
001002
001002001
001002002
001003
001003001
001003001001
001003001002
001003001003
001003002
001003002001
001003002002

By using the serial number, you can take any node and find out its exact position and know all the categorys it took to get to it... how? Lets take:

001001001001

By looking at this serial, we know it took 4 steps to get to it... 001001001001 (001|001|001|001)... now, lets see how to get to it...

001001001001

Lets say we want to find out the category(s) above it... some code like:

<%
serial = "001001001001"
serialcount = len(serial)
x = 0
Do Until x = serialcount
x = x + 3
Response.Write right(serial, x) & "
"
Loop
%>

This will take the serial 001001001001 and output

001
001001
001001001
001001001001

Q: What if i want to find out whats under the category "Online Store"?
A: This can be done in the SQL by using LIKE... In my instructions, the serial number for "Online Store" is 001003, so the statement to see categories below it would be:


SELECT * FROM table Where serial like '001003%' ORDER BY serial

And this would output from my example structure.

001003
001003001
001003001001
001003001002
001003001003
001003002
001003002001
001003002002

What if I only want the nodes below "Online Store" and not go any further? Well, that is accomplished by adding another field calling it "ParentCatID"... this field will hold the database assigned category id #... Lets say that "Online Store" is ID# 3... All categorys below it would have a ParentCatID of 3... That way you can run an SQL like this:

SELECT * FROM table Where ParentCatID = '3' ORDER BY serial

This would Output:

001003001
001003002

Want to know how to change the sort order? I will have an article up soon on a very easy way of doing this. Check back for updates.
This is something i came up with about a couple of years ago... figured i would release it now. See how it works for your project(s)!

Remember: This is an idea for an easy way to handle a data tree. If you do not appreciate my idea, then you are not dealing with a tree problem or never worked for one. My data tree idea is 100% functional now and is used in a variety of my projects from search engines to database driven websites wanting a dynamic site tree.

If you like this idea and use it... GIVE ME A HIGH RATING! I did go through some trouble to put it on here to help you... Thanks...

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzipto decompress it.

Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:
1)Re-scan downloaded files using your personal virus checker before using it.
2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
3/15/2002 11:04:41 AM:Stefan
Unlimited ?? - What happens if there are more than one thousand nodes within one node??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/18/2002 10:44:21 PM:Lewis Moten
Cool idea to start with, but here is another. Create two fields - ListIndex and Depth. Should be quicker to search by numbers. Still, you have a really cool solution. I'll give you 5 spheres.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/19/2002 11:22:38 AM:Michael V Feranda
In my instructions, i give a 3 char set... you can increase that to 4 5 6... whatever you want to deal with.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/18/2002 3:12:31 AM:Observer
Hi, about kind of this way I thinked few years ago. Other way which I use is to every tree Item(with unique ItemId) to set its ParentID(the Id of the upper level) and to "walk" on the tree with racursion like "select * from TreeItems where ParentId=..." May I can post this script soon
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/18/2002 3:18:04 AM:Michael V Feranda
yeah, the only problem with doing it that way (which i used to) is that you have to keep running a query... the thing about the serial number is that you run ONE query and it drops the entire tree IN ORDER... and unlike the parentID method, you can look at the serial number and see EXACTLY where its located in the tree... you know how many steps it takes to get to it and such
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/18/2002 9:32:03 AM:BigYak
Five points to you! Great idea, I'm going to start using it immediately for a message board application. If I meet you in a bar, I'll buy you a beer. One thing - it seems a piece of the code is missing above -- where it says "Some code like:" -- what was that?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/29/2002 9:10:28 PM:Keir Gordon
Nice, Very Nice. You've got my vote.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/17/2002 2:06:27 AM:frumbert
i create categories using a parentid and lookup. i look up the whole tree or just a branch under an id (you do a lookup for all branches whose parent id matches the id of the branch you want). i pop all this into an array with 4 items - the id, the name, the parent id and the depth. this way i can close off the recordset and use the array over and over, and you have everything you need - a structure, a depth, and the id's. maybe not the most efficient way, but it works a treat.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.