Advertisement
1_2002 Coding Standards #112380

Unlimited Depth Category Tree

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.

AI

Ringkasan AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kode Sumber
original-source
<table width="100%" border="1" cellspacing="0" cellpadding="10" bordercolor="#FF9900">
 <tr>
 <td background="http://64.191.16.38/images/background.jpg">
 <table width="100%" border="1" cellspacing="0" bordercolor="#FF9900">
 <tr>
 <td bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>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.<br><br>This is VERY easy to 
 do!!<br>If you download the zip file, you will find a example database and code.<br>
 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.<br>
 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.</b><br>
 <br>
 Home - 001<br>
 <br>
 <b>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.</b><br>
 <br>
 </font></p>
<table width="400" border="1" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF">
 <tr> 
 <td bordercolor="#000000"> 
 <table width="400" border="0" cellspacing="0" cellpadding="0">
 <tr> 
 <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2">Home</font></td>
 <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2">001</font></td>
 </tr>
 <tr> 
 <td bgcolor="#FFFFCC"><font face="Arial, Helvetica, sans-serif" size="2"> About 
 Us</font></td>
 <td bgcolor="#FFFFCC"><font face="Arial, Helvetica, sans-serif" size="2">001001</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2"> Contact 
 Us</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001002</font></td>
 </tr>
 <tr> 
 <td bgcolor="#FFFFCC"><font face="Arial, Helvetica, sans-serif" size="2"> 
  Online Store</font></td>
 <td bgcolor="#FFFFCC"><font face="Arial, Helvetica, sans-serif" size="2">001003</font></td>
 </tr>
 <tr> 
 <td> 
 </table>
 </td>
 </tr>
</table>
<p><font face="Arial, Helvetica, sans-serif" size="2"><br>
 <b>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.<br>
 Lets add a few more nodes.</b><br>
 </font></p>
<table width="400" border="1" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF">
 <tr> 
 <td bordercolor="#000000"> 
 <table width="400" border="0" cellspacing="0" cellpadding="0">
 <tr> 
 <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2">Home</font></td>
 <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2">001</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2"> About 
 Us</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001001</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">  Company 
 Profile</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001001001</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Stock 
 Market</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001001001001</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">   Map 
 to us</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001001002</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2"> Contact 
 Us</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001002</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">  Mailing 
 Address</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001002001</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">   Phone 
 Information</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001002002</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">  Online 
 Store</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">   Computer 
 Related</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003001</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Hard 
 Drives</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003001001</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Software</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003001002</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Motherboards</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003001003</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">   Furniture</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003002</font></td>
 </tr>
 <tr> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Computer 
 Desks</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003002001</font></td>
 </tr>
 <tr bgcolor="#FFFFCC"> 
 <td><font face="Arial, Helvetica, sans-serif" size="2">    Office 
 Chairs</font></td>
 <td><font face="Arial, Helvetica, sans-serif" size="2">001003002002</font></td>
 </tr>
 <tr> 
 <td> 
 </table>
 </td>
 </tr>
</table>
<p> 
<font face="Arial, Helvetica, sans-serif" size="2"><br>
<b>Now, with each of those items in your database, if you did your SQL statement 
as:<br>
</b>SELECT * FROM table ORDER BY serial<b><br>
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:</b><br>
<br>
001 = 1<br>
001001 = 1,001<br>
001001001 = 1,001,001<br>
<br>
<b>we dont want that, by it sorting in abc order... it looks at each character 
and sorts by greatest of that character. Example:</b><br>
<br>
001<br>
001001<br>
001001001<br>
001001001001<br>
001001002<br>
001002<br>
001002001<br>
001002002<br>
001003<br>
001003001<br>
001003001001<br>
001003001002<br>
001003001003<br>
001003002<br>
001003002001<br>
001003002002 </font> 
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>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:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">001001001001</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>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... </b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">001001001001</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Lets say we want to find 
 out the category(s) above it... some code like:</b></font></p>
      <p><font face="Arial, Helvetica, sans-serif" size="2">serial = "001001001001"<br>
       serialcount = len(serial)<br>
       x = 0<br>
       Do Until x = serialcount<br>
       x = x + 3<br>
       Response.Write right(serial, x)<br>
       Loop </font></p>
      <p><font face="Arial, Helvetica, sans-serif" size="2"><b>T</b></font><font face="Arial, Helvetica, sans-serif" size="2"><b>his 
       will take the serial 001001001001 and output</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">001<br>
 001001<br>
 001001001<br>
 001001001001<br>
 </font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Q: What if i want to 
 find out whats under the category "Online Store"?<br>
 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:</b><b><br>
 <br>
 </b>SELECT * FROM table Where serial like '001003%' ORDER BY serial<b></b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>And this would output 
 from my example structure.</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">001003<br>
 001003001<br>
 001003001001<br>
 001003001002<br>
 001003001003<br>
 001003002<br>
 001003002001<br>
 001003002002 </font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>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... </b></font><font face="Arial, Helvetica, sans-serif" size="2"><b>That 
 way you can run an SQL like this:</b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2">SELECT * FROM table Where 
 ParentCatID = '3' ORDER BY serial</font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>This would Output:<br>
 <br>
 </b>001003001<b><br>
 </b>001003002<b><br>
 </b></font></p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>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.</b><br>
 <b>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)! <br><br>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. <br><br>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...</b></font></p>
</td>
 </tr>
 </table>
 </td>
 </tr>
</table>
Komentar Asli (3)
Dipulihkan dari Wayback Machine