NYCPHP Meetup

NYPHP.org

[nycphp-talk] Building trees

Jim Hendricks(Biz Computing) jim at bizcomputinginc.com
Wed Oct 16 14:26:18 EDT 2002


Yes, I think if I get your meaning.  The app is a broadbased app for 
managing my business and extending certain parts to involved parties ( 
ie. my clients and sub contractors ).  The specific table in question is 
projects where projects can have subprojects which can have 
subprojects... as deep as you want.  So with my table design you might see:

ID  ParentID  Name
1     0             The greatgrandaddy project
2     1             grandpa 1
3     1             grandpa 2
4     2             pa 1
5     2             pa 2
6     3             pa 3
7     3             pa 4
8     5             child 1
9     5             child 2
10   5             child 3

The results of the query would lead to a display like:

The greatgranddaddy project
     grandpa 1
          pa 1
          pa 2
               child 1
               child 2
               child 3
     grandpa 2
          pa 3
          pa 4

I had thought about using the relationship table, but didn't see how 
that would make the query simpler.

A straight query of my original design will get me all the data, the 
problem is parsing that data so I can lay it out in a tree format.  If 
there were a way within the query where I can order the data on 
greatgranddaddy, granddaddy, daddy, child then it could be done in 1 
query.  But this then also assumes a tree only 3 levels deep ( which is 
plenty for this situation, but I may need this solution in other places 
as well where 3 may not be deep enough ).  This also assumes ordering by 
id's but ultimately I want it ordered by name within each tier.

Jim

bruce at mtiglobal.com wrote:

>Can a parent by a child and a child be a parent?
>
>Is so, perhaps you could have another table where the relationship is
>established.
>
>tblObjects:
>objectID
>fname
>lname
>etc. fields
>
>tblRelationships:
>relationship  - autonumber
>parent
>child
>
>where parent and child are the id's from tblObjects.
>
>I could probably be more clear if I knew the specifics of what the
>entities you are using are.
>
>>From this you could then select from the two tables.
>
>- Bruce
>
>  
>
>>I know this is a SQL question, but since most apps deal with SQL, I
>>figured I'ld ask here since the app is using PHP and MySQL.
>>
>>I have a table that establishes an entity that may have a parent from
>>the same table.  I would like to display the data in a tree format where
>> all items with a ParentID of 0 ( no parent ) are listed with children
>>indented under the parent.  The nesting can be infinite, but
>>realistically will only be 2 or 3 levels deep.  I can see how to do this
>> through a whole series of queries, but can see how the performance of
>>such a design could be very poor if there is a lot of items.  Looking
>>for any ideas, even if they would require a table design mod to make the
>> queries simpler.  The current design is a single table with ID, and
>>ParentID and whatever other info needed for the item.
>>
>>Thanks, Jim
>>
>>
>>
>>
>>
>>
>>
>>    
>>
>
>
>
>
>
>--- Unsubscribe at http://nyphp.org/list ---
>
>
>
>
>
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20021016/b4314ef0/attachment.html>


More information about the talk mailing list