I have in database people with relation to parents and I need to create recursive function which return array with genealogical tree. Database example:
NUMBER;NAME;FATHER;MOTHER
001;Name1;002;005
002;Name2;007;018
003;Name3;018;025
005;Name5;006;019
023;Name23;019;045
018;Name18;062;097
007;Name7;;-
...
I prepared two functions - first for man detail and second recursive for search ancestors.
I need this result:
$out[0][0] // first person
$out[1][0] // mother of first person
$out[1][1] // father of first person
$out[2][0] // grandmother of first person (mother side)
$out[2][1] // grandfather of first person (mother side)
$out[2][2] // grandmother of first person (father side)
$out[2][3] // grandmother of first person (father side)
...
next generation have 8 items, next 16 items, ... Maximum is 6 generations back.
I prepared two functions - first for man detail and second recursive for tree building. In second function
define("MAX_GEN",5);
function detail($number) {
$d = mysql_query("select * from table where number = '$number'");
if(mysql_num_rows($d) == 0) {
$p[name] = "N/A";
$p[number] = "N/A";
$p[m_number] = "N/A";
$p[f_number] = "N/A";
}
else $p = mysql_fetch_assoc($d);
return $p;
}
function gen($number, $generation = 0, $out) {
if ($generation >= MAX_GEN) {
return false;
}
$record = detail($number);
if ($generation == 0) $out[0][] = $record; // first man
$generation++; // next generation
if (!$out[$generation] && ($generation != MAX_GEN)) $out[$generation] = array();
$x_mother = gen($record[m_number], $generation ); // continue with mother
$x_father = gen($record[f_number], $generation ); // continue with father
if ($out[$generation]) {
$out[$generation][] = $x_mother;
$out[$generation][] = $x_father;
}
return $out;
}
But in second function is problem - I don't know how pass array with results to next and next generation. It's still return one element or (after few attempts) it returns array which I want but with inserted arrays between generations and result is unusable. Can anyone help me please?
Example of result which I wand is here: http://www.foxterrier.cz/_TM_importy/example.php