PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
<?
/*
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>";
?>
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.
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.
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.
>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