NYCPHP Meetup

NYPHP.org

[nycphp-talk] Logic Help

cliff cliff at pinestream.com
Thu Oct 27 12:36:05 EDT 2005


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



More information about the talk mailing list