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...
|