【问题标题】:how to show binary tree data in table如何在表格中显示二叉树数据
【发布时间】:2023-04-01 01:48:02
【问题描述】:
    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);

这是模型/功能

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;
}

我们正在研究二叉树计划结构,数据在 foreach 循环中获取得非常好,但是当我们试图在表中显示数据时它无法正常工作。它只显示总数据的一半,有时甚至不显示一半的数据。帮我解决这个问题,提前谢谢......

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您正在使用多个叠瓦状 foreach 来显示数据,因此并未真正遍历所有树,因此看不到所有数据。

    我建议你检查PHP Binary Search Tree, How to Traverse

    【讨论】:

    • 不行,我想在表格中显示树结构
    • 这就是你想要的,但不是你正在做的:) 你应该做的是使用遍历算法遍历树,以便能够按顺序到达树的所有分支显示它。当您使用数据库时,我无法真正提供完整的代码答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多