1

I just need a php code which retrieves data from mysql and build a family tree.

Sql data has the following schema.

(Id, Name, ParentId1, ParentId2)  

I'll insert the data as

INSERT INTO table_name (`Id`, `Name`, `ParentId1`, `ParentId2`)
VALUES ('100', 'Name_1', 'Parent_1', 'Parent_2')

If 0 is specified for parentId1 and ParentId2 then he is in top of the family. The code should generate the family tree based on the parentId1 and parentId2. Based on the matching parentId, name should attach to the parent. Optionally If I specify a relation between two example marriage, they should be connected in the code. Its ok if you just tell me an overview of how to design or approach.

Jayaprakash
  • 11
  • 1
  • 2

1 Answers1

0

I use this function to do this. This function I have created return a table whit a linear data tree

You can follow the idea and customize to you

static function display_child_nodes($parent_id, $level,$data,$index,$dr){//first level=  NULL,0
       global $data, $index,$dr;

       if(is_null($parent_id)){
            $parent_id ="NULL";
       }

       if (isset($index[$parent_id])) {           
            foreach ($index[$parent_id] as $id) {
                $mark=NULL;
                if($level<1){
                    $content = $data[$id][PROCEDIEMNTOS_PROFISSIONAIS_NOME];

                }
                else{
                    $content =str_repeat("&nbsp", $level*2) .$data[$id][PROCEDIEMNTOS_PROFISSIONAIS_NOME];
                }
                $dr[$id]=$content;
                self::display_child_nodes($id, $level + 1,$data,$index,$dr);
            }        
    }    
    return $dr;   
}
IgorAlves
  • 5,086
  • 10
  • 52
  • 83