Page 1 of 1

Family Tree code :Help needed

Posted: Mon Oct 18, 2004 5:34 am
by just_ht
feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I need to implement  simple Family Tree  ,Taking value from DB(Mysql)
I need to implement it for as many level as..,
Any Idea lease help

here is code:

Code: Select all

<? 
/* 
Input this in your mySql db  before running this 
it allows you to get 99 heads  and  1--99  categorie in each node 
Warning code only 3 levels, need to improve it
# Table structure for table 'res_cat' 
CREATE TABLE res_cat ( 
   ID int(32) DEFAULT '0' NOT NULL , 
   ID_uname char(40) NOT NULL, 
   UNIQUE ID (ID) 
); */

# Dumping data for table 'res_cat'
$mysql_link = mysql_connect("localhost", " username", " password"); 
mysql_select_db("res_cat", $mysql_link); 
echo "<html><body>" ; 

function listrub ($id , $exploseID   ) 
{ 
global $PHP_SELF ; 
global $mysql_link  ; 
$maxi = ($id*100)+100 ; 
$mini = $id*100 ; 
$query = "SELECT * FROM res_cat  where ID < $maxi and ID >  $mini    " ;

echo "<ol>"; 
if(      $mysql_result = mysql_query($query , $mysql_link)   ) 
{ 
while ($row = mysql_fetch_object($mysql_result)) 
    { 
// you have to link some action on leaves of course 
echo "<li> <a href='$PHP_SELF?ID_rub=$row->ID' >   $row->ID_uname </a>  </li> " ; 
$testID = ($exploseID - ($exploseID  %100 ))/100 ; //  WARNING this is ugly .. only 3 level
if ( $testID == $row->ID || $exploseID == $row->ID) 
         { 
         listrub ($row->ID , $exploseID ) ; 
        } 
    } 
} 
echo "</ol>"; 
} 
$father =999999999;  // a big number   
// get the grand...grand father 
if ( $ID_rub >100 ) 
{ 
$father = $ID_rub ; 
while ( $father >100 )    { $father= ( $father - ( $father %100) )/100 ; } 
} 
else{$father= $ID_rub ;} 
listrub ($father , $ID_rub ,$mysql_link ) ;   
echo "</body></html>"; 

?>

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 18, 2004 6:07 am
by Sema
hmm i know that the admins/mods will tell you this, but you need to use the -

Code: Select all

  [/php ]- tags around your code in this forum, it is better for the users of the forum...  :P

Posted: Mon Oct 18, 2004 6:50 am
by vigge89
also, why do u only post some of the code in each reply? :S

Posted: Mon Oct 18, 2004 11:15 am
by feyd
for future reference, there wasn't a need to have 4 posts just for the code.

As for your "problem", as vague as it is, recursion is one basic way to do it. it looks like you have the beginnings of how to do it, but will likely run out of memory quickly. This may be a bit complicated for a beginner to be working on. Maybe you can figure out a different way.

Posted: Mon Oct 18, 2004 11:17 am
by timvw
shouldn't be to hard to type "tree in sql" in a search engine.

pretty sure you will find thingies like nested sets etc.... even this forum has quite some threads on the issue...

Posted: Mon Oct 18, 2004 11:53 pm
by just_ht
Thanks for all that stuff,I think I got some basic idea to do it now but it will be really helpful if any one can
provide some code snippets to sort out the problem.

Posted: Tue Oct 19, 2004 12:01 am
by feyd
one of ways I've done this sort of thing in the past is through parsing the current level before any sublevels. Whenever I came across a level that had sublevels I stored off a reference to that level in another array that I used as a level list of sorts.

This is very similar to doing recursive directory/folder traversal.

Posted: Tue Oct 19, 2004 6:05 am
by just_ht
hELLO fEYD

>one of ways I've done this sort of thing in the past is through parsing the current level before any sublevels. Whenever I came across a level that had sublevels I stored off a reference to that level in another array that I used as a level list of sorts

>>cAN U PLEASE POST THE CODE; THANKS IN ADVANCE

Posted: Tue Oct 19, 2004 9:14 am
by feyd
I won't, because I feel you should be able to figure this out on your own with some tips from us, among others...