Re: Category and sub-category logic

From: Date: Fri, 15 Aug 2003 15:13:26 +0000
Subject: Re: Category and sub-category logic
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-159703@lists.php.net to get a copy of this message
From: "Mark" <mark_weinstock@yahoo.com> > I'm trying to get a handle on this algorithm as well. If you add > additional children to a parent record, doesn't that require > renumbering all the rgt and lft values in the lineage? For example > (table shamelessly stolen from a website): > > Personnel > emp lft rgt > ====================== > 'Albert' 1 12 > 'Bert' 2 3 > 'Chuck' 4 11 > 'Donna' 5 6 > 'Eddie' 7 8 > 'Fred' 9 10 > > If I want to add 'Joe' as a subordinate to 'Fred', doesn't that mean > I have to change the lft values for Fred, Chuck, and Albert? Yes, > deleting definitely seems easier. I'm curious about adding... Yes, you will have to update all of the right and left numbers to the right (or higher) than the node where you are inserting. Deleting is basically the same method done in reverse, but can get complicated when you delete a node and have to determine what to do with the children. I'd recommend, if you're really interested in this, to do some searching on nested set tutorials. You could specifically search for Joe Celko who has written a lot of information on the topic. While it seems like a big deal to have to update all of the numbers, it's really not. It can be done with a single query (with subselects). Yes, it's more costly than inserting a name into an adjacentcy list model, but it's worth it for all that you gain from the nested set model. ---John Holmes...

« previous php.general (#159703) next »