Re: Brainiacs please help! PostgreSQL and foreach() problem
| From: | Raditha Dissanayake | Date: | Wed, 10 Sep 2003 04:22:58 +0000 |
| Subject: | Re: Brainiacs please help! PostgreSQL and foreach() problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-162137@lists.php.net to get a copy of this message | ||
Hi,
It's not very clear what you are trying to do. From what i understand from your mail, i think a recursive call to select_names would be the ideal solution. In these scenario you call select_names() once and from within the function you call select_name() again for the each of the matches on the first level.
You should be aware of the obvious drawback of recursive calls: memory consumption. If your tree strecthes to infinity as you feel it migh, you will get a stack overflow. :-)
Peter Sharpe wrote:
I have a database full of names. each name could be linked to any number of sub-names, each sub-name could be linked to any number of sub-sub-names, to infinity (unlikely but possible). I need to iterate through this nest of names starting with a main name; lets call the main name Peter. Peter could have John, Tim & Mike working for him. Tim could have Greg working for him. function select_names($current_top_name){ global $dbh; $sql = " SELECT name_id_fk FROM name_relation WHERE top_name_id_fk = '".$current_top_name."' "; $rs = pg_query($dbh, $sql); if(($num_rows = pg_num_rows($rs)) > 0){ for($i=0;$i<$num_rows;$i++){ $row = pg_fetch_row($rs, $i, PGSQL_ASSOC); $associated_names[] = $row['name_id_fk']; } pg_free_result($rs); } // end if(($num_rows = pg_num_rows($rs)) > 0) return $associated_names; } // end function select_names() $current_top_name = 'Peter'; while(!$stop){ $assoc_names = select_names($current_top_name); foreach($assoc_names as $key => $val){ print($val); $more_assoc_names = select_names($val); if($more_assoc_names){ ARG HELP IM NOT SMART ENOUGH } } // end while(!$stop) } // end foreach($assoc_names as $key => $val)-- http://www.radinks.com/upload Drag and Drop File Uploader.