Re: "Nested Set Model" or "modified preorder tree traversal"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hasn't anyone else done this before (recently)? Did everyone just write their own code every time?
Anyone have some easy to use code that allows for:
Add, delete, update/rename, select, show tree, bread crumb display, etc.

I use PHP 5.0.5 and mySQL 5.0.18, so ideally it would take advantage of all
the optimizations and bells & whistles of those more modern versions.

The php or mysql version doesn't matter, it's more to do with how you structure your database.

Instead of your id/parentid relationship the way I did it was you create a 'node' with this information in it.

So:

select categoryid, node, categoryname from all_categories;
 categoryid |   node   | categoryname
------------+----------+--------------
          9 | 0009     | Parent
         10 | 0010     | Another Parent
         11 | 00090011 | Sub Cat
         13 | 00100013 | Sub Cat 2
         12 | 00100012 | Sub Cat 3

catid '11' is a subcategory of '9'.
catid '13' is a subcat of '10'.

You'll need to pad each node out to a certain length so you don't get overlaps (in my case I used 4 giving me 9999 categories at each level). The longer each node is the more categories you can have at each level but the smaller number of categories you have altogether.

Don't forget mysql & other databases only let you index up to 255 characters of a text field (in my case I used a varchar(200) for the node field), so while you could in theory have infinite levels it's not really practical (unless you use full text indexing).


Inserting is easy - you get the parent that you're inserting under and add your padded node on the end of it giving you the full path.


Deleting is easy as well - to delete a category and all subcategories:

delete from all_categories where node like '0009%';


Moving things around is harder..


If you need more info let me know.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux