RE: [PHP] Creating tree using table?

From: Date: Fri, 23 Mar 2001 09:56:48 +0000
Subject: RE: [PHP] Creating tree using table?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-45261@lists.php.net to get a copy of this message
This is a big topic. I'll be brief. This all depends upon your data structure. There are a couple of ways that you can store information like this. The classic (purist) method would be to have a table of the following type: table_Tree Node ID | Parent ID | Node Data ==========+===========+========== 1 | 0 | xxx 2 | 1 | xxx 3 | 1 | xxx This describes a tree where node 1 is the top (parent id = 0) and has 2 children (nodes 2 and 3). This approach can be used to describe any depth of tree with any number of child nodes. You then need to decide how to represent this in PHP. You could create a node class that contains a collection of child nodes, a reference to the parent node and the node data. You could then construct a physical representation of your tree in PHP. You could then write some routines to traverse your tree and count depth level of any particular node. Alternatively, you could store the data in the db table in the same way in PHP (as an array) and then write some routines to traverse your array. Both approaches are valid and work. You would have to write the code to count the depth of a node. You could also store you information in the database as: table_Tree Node ID | Depth | Node Data ==========+===========+========== 1 | 0 | xxx 2 | 1 | xxx 3 | 1 | xxx 4 | 0 | xxx Making the assumption that the rows are ordered correctly. By reading row-by-row you can assume that if a following row is at a lower depth or the same, then you are in the same branch of the tree. As soon as the depth is at a higher level then you have traversed back up a branch. You can store this representation directly in PHP in arrays and then write some routines to render/handle your tree however you like. This approach will give you a depth level directly, however, it's not as 'nice' as the first types of trees as it relies upon order rather than strong relationships with child nodes. Your choice. Hope that helps, Neil -----Original Message----- From: E K L [mailto:phpnewuser@hotmail.com] Sent: 23 March 2001 08:29 To: php-general@lists.php.net Subject: [PHP] Creating tree using table? Hi all, Currently, i'm trying to write a program which is related to tree manipulation. Meaning that, i'll search the tree to get level or count how many child exist for particular parent, for example. Initially i planned to use table to represent all kind of datas that are parent, left child and right child. However, i still face problem in calculating the level of the tree. So, can any one gives me an idea on how to manipulate the table so that i will be able to count the level? Your prompt reply is much appreciated. Thanks.... E K _________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#45261) next »