NYCPHP Meetup

NYPHP.org

[nycphp-talk] Logic Help

Roland Cozzolino rcozzol at optonline.net
Thu Oct 27 12:49:38 EDT 2005


I agree completely (also called nested sets).  This can be implemented 
in both php and the DB for excellent performance, truly unbalanced trees 
and unlimited depth of parent-child.
You would end up with your object (id, name) and a left and right side 
numeric value.  A child is something where its left side > parents left 
and its right side < parents right
here is a poor textual representation (left side, right side):

            (1 ,10)
               |
         --------------
         |            |
      (2,7)        ( 8,9)
        |
    ----------
    |        |
(3,4)       (5,6)

cliff wrote:

>id/parent id is a performance nightmare.
>
>I use a Modified Preordered Tree Traversal (add left, right fields).
>Google Modified Preordered Tree Traversal
>Try: http://www.philbrodeur.com/tutorials/mptt/ to start
>
>Cliff Hirsch
>
>On Thu, 27 Oct 2005 12:12:25 -0400, Jeff Loiselle wrote
>  
>
>>Hello,
>>
>>I will submit that am really just not smart enough to figure this 
>>out. My logic sucks. Why am I a programmer? I don't know. Someone 
>>must have drugged me the day I picked my career. But I have records 
>>in a database that look like this..
>>
>>array(
>>  'id',
>>  'name' ,
>>  'parent_id'
>>)
>>
>>I want to build a tree.
>>
>>So far I have..
>>
>>while ($record->fetch()) {
>>  $tree[$record->parent_id][$record->id] = $record->name;
>>}
>>
>>That produces something like this.
>>
>>Array
>>(
>>    [] => Array
>>        (
>>            [1] => General
>>        )
>>
>>    [1] => Array
>>        (
>>            [2] => Diseases
>>            [5] => Technologies
>>            [6] => Financial Data
>>        )
>>
>>    [2] => Array
>>        (
>>            [3] => Eg 1
>>            [4] => Eg 2
>>        )
>>
>>    [6] => Array
>>        (
>>            [7] => Articles
>>            [8] => Models
>>            [9] => Comps
>>        )
>>
>>)
>>
>>Now how can I go about appending each node to its matching parent? Or
>>is there a completely better way?
>>
>>Have I made any sense? ;-)
>>
>>---
>>Jeff Loiselle
>>Web Developer, Musician, and Observer
>>http://jeff.loiselles.com
>>_______________________________________________
>>New York PHP Talk Mailing List
>>AMP Technology
>>Supporting Apache, MySQL and PHP
>>http://lists.nyphp.org/mailman/listinfo/talk
>>http://www.nyphp.org
>>    
>>
>_______________________________________________
>New York PHP Talk Mailing List
>AMP Technology
>Supporting Apache, MySQL and PHP
>http://lists.nyphp.org/mailman/listinfo/talk
>http://www.nyphp.org
>
>  
>



More information about the talk mailing list