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($c urrent_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<$nu m_rows;$i++){
$row = pg_fetch_row($r s, $i, PGSQL_ASSOC);
$associated_nam es[] = $row['name_id_fk'];
}
pg_free_result( $rs);
} // end if(($num_rows = pg_num_rows($rs )) > 0)
return $associated_nam es;
} // end function select_names()
$current_top_na me = 'Peter';
while(!$stop){
$assoc_names = select_names($c urrent_top_name );
foreach($assoc_ names as $key => $val){
print($val);
$more_assoc_nam es = select_names($v al);
if($more_assoc_ names){
ARG HELP IM NOT SMART ENOUGH
}
} // end while(!$stop)
} // end foreach($assoc_ names as $key => $val)
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($c urrent_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<$nu m_rows;$i++){
$row = pg_fetch_row($r s, $i, PGSQL_ASSOC);
$associated_nam es[] = $row['name_id_fk'];
}
pg_free_result( $rs);
} // end if(($num_rows = pg_num_rows($rs )) > 0)
return $associated_nam es;
} // end function select_names()
$current_top_na me = 'Peter';
while(!$stop){
$assoc_names = select_names($c urrent_top_name );
foreach($assoc_ names as $key => $val){
print($val);
$more_assoc_nam es = select_names($v al);
if($more_assoc_ names){
ARG HELP IM NOT SMART ENOUGH
}
} // end while(!$stop)
} // end foreach($assoc_ names as $key => $val)
Comment