NYCPHP Meetup

NYPHP.org

[nycphp-talk] Logic Help

Roland Cozzolino rcozzol at optonline.net
Thu Oct 27 12:32:21 EDT 2005


One solution is to change your DB design, but I imagine that is not an 
option.
That being said, if you are using balanced trees, you can implement a 
b-tree algorithm.
If you are using unbalanced trees (meaning, nodes can be any number, any 
depth, etc.) you could opt for a binary tree.

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