I am getting duplicate nodes on my family tree

妖精的绣舞 提交于 2019-12-10 22:14:30

问题


My function is duplicating some nodes on the tree. When I am calling that function in a recursive way. I have a database table of all father, mother, wife, husband, child ids. I am taking all persons from their father id only. I don't know from where these extra nodes are coming and how should I handle ie it. Output image

   <?php
    function categoryTree($parent_id = 0,&$alreadyThereArr=array())
    {

        $ci =& get_instance();
        //$query = $ci->db->query("SELECT * FROM tbl_user WHERE father_id = $parent_id ORDER BY id ASC");
        $sql="SELECT * FROM `tbl_user` WHERE father_id = '$parent_id' ORDER BY id ASC";
        $query=$ci->db->query($sql);
        $result=$query->result();

        if(count($result) > 0)
        {
            echo '<ul>';
            foreach($result as $key)
            {
                if(!in_array($key->id,$alreadyThereArr))
                {
                    //echo 'In';
                    if($key->wife_id != 0)
                    {
                        //echo 'wife';
                        $wife=$ci->Main_M->getSingleRow('tbl_user','id',$key->wife_id);

                        $start='<div>
                                    <span class="male">'.$key->name.'</span>
                                    <span class="spacer"></span>
                                    <span class="female">'.$wife->name.'</span>
                                </div>';
                    }
                    else
                    {
                        $gender=($key->gender == 'M') ? 'male' : 'female';
                        $start='<div><span class="'.$gender.'">'.$key->name.'</span></div>';
                    }

                    $alreadyThereArr[]=&$key->id;
                     echo '<li>';

                         echo $start;

                             echo '<ul>';

                                 categoryTree($key->id, $alreadyThereArr);

                             echo '</ul>';

                     echo '</li>';
                }
            }
            echo '</ul>';
        }
         //print_r($alreadyThereArr);
    }
    ?>

来源:https://stackoverflow.com/questions/56730668/i-am-getting-duplicate-nodes-on-my-family-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!