0
    public function left_team_count2()
     {
       $right = $this->register->left_team();
        echo '<table border="1">
        <tr>
        <th>name</th>
        <th>id</th>
        </tr>
        <tbody>'; 
        $i = 1; foreach($right as $row)
        {

        echo '<tr>
        <td>'.$i.'</td>
            <td>'.$row['name'].'</td>
            <td>'.$row['user_id'].'</td>';
                while(!empty($row['child'])){

                foreach($row['child'] as $row)
                {

                    echo '<tr>
                        <td>'.$i.'</td>
                        <td>'.$row['name'].'</td>
                        <td>'.$row['user_id'].'</td>
                         </tr>
                        ';
                     while(!empty($row['child'])){

                            foreach($row['child'] as $row)
                            {

                                echo '<tr>
                                <td>'.$i.'</td>
                                <td>'.$row['name'].'</td>
                                <td>'.$row['user_id'].'</td>
                                </tr>';

                           $i++; }
                        }
            $i++;}
        }
            echo '</tr>';
           $i++; }

        echo '</tbody>
    </table>';

    echo '<pre>';
    print_r($right);

this is model/function

function left_team()
{ 

    $result = $this->db->select('user_id,name,time,activation,status')->from('login')->where('side','Left')->where(array('sponser'=>$this->session->userdata('username')))->get()->result();
        $employee = array();
        foreach($result as $data){
            $emp = array();
            $emp['user_id']=$data->user_id;
            $emp['name']=$data->name;
            $emp['time']=$data->time;
            $emp['activation']=$data->activation;
            $emp['status']=$data->status;
            $emp[] = $data->user_id;
            $emp['child'] = $this->left_count($emp);
            array_push($employee,$emp);
        }
        return $employee;

}

function left_count($emp)
{
        $this->db->where_in('sponser',$emp);
        $q = $this->db->get('login');
        $tree = $q->result();    
        $employee = array();
        foreach($tree as $data){
            $emp = array();
            $emp['user_id']=$data->user_id;
            $emp['name']=$data->name;
            $emp['time']=$data->time;
            $emp['activation']=$data->activation;
            $emp['status']=$data->status;
            $emp[] = $data->user_id;
            $emp['child'] = $this->right_count($emp);
            array_push($employee,$emp);
        }
        return $employee;
}

We are working on a binary tree plan structure, the data is fetching very well in the foreach loop but when we are trying to show the data in the table it not working properly. It's showing only half of the total data or sometimes not even the half of the data. Help me to get rid of the problem, thank you in advance.....

1 Answers1

0

You are using multiple imbricated foreach to display you data, hence not really traversing all of the tree and thus not seeing all the data.

I recommend you check PHP Binary Search Tree, How to Traverse

smwhr
  • 675
  • 6
  • 22
  • it's not work i want to show tree strcuture in table – Yash Jaiswal Nov 19 '19 at 12:23
  • That's what you want for sure, but not what you are doing :) What you should be doing, is traverse the tree using a traversal algorithm to be able to reach all the branches of your tree in order to display it. As you use a database, I cannot really provide a full-fledged code answer. – smwhr Nov 19 '19 at 13:04