Breadth-first traversal or depth-first traversal. This is one possible implementation of a depth-first traversal in a database table: http://www.sitepoint.com/article/hierarchical-data-database/
-Matt On Thu, Jun 11, 2009 at 3:15 AM, iceangel89 <comet2...@gmail.com> wrote: > > if i have a table like > > Nodes > =========== > id > name > parent (ref Nodes.id) > > how shld i iterate thru it? i am using PHP. wondering if theres an > efficient > way ... > > i did it something like > > <pre> > mysql_connect("localhost", "root"); > mysql_select_db("test"); > > $arr = array(); > > $sql = "SELECT * FROM nodes WHERE parent IS NULL ORDER BY name"; // to > select root level nodes > $rs = mysql_query($sql) or die(mysql_error()); > while ($row = mysql_fetch_assoc($rs)) { > $row['children'] = get_children($row); > $arr[] = $row; > } > //echo "<pre>"; > //print_r($arr); > //echo "</pre>"; > echo print_list($arr); > > function get_children($row) { > $sql = "SELECT * FROM nodes WHERE parent = $row[id] ORDER BY name"; > // > select child rows > $rs = mysql_query($sql) or die(mysql_error()); > $arr = array(); > while ($row = mysql_fetch_assoc($rs)) { > $row['children'] = get_children($row); > $arr[] = $row; > } > return $arr; > } > </pre> > -- > View this message in context: > http://www.nabble.com/Iterating-a-Tree-tp23978662p23978662.html > Sent from the Zend Framework mailing list archive at Nabble.com. > >