Codeigniter递归函数

时间:2016-04-29 06:38:36

标签: php mysql codeigniter

我有一个帐户部分,下面提到了结构

Assets
     Group
       Accountname
     Group
       Group
         accountname
         Group
           accountname
           accountname
           Group
            account
Etc....

用户可以在每个组下创建多个帐户和组。 我维护数据库,

    CREATE TABLE IF NOT EXISTS `tbl_account_details` (
`account_details_id` int(11) NOT NULL,
  `account_code` varchar(50) NOT NULL,
  `account_name` varchar(100) NOT NULL,
  `IsGroup` int(11) NOT NULL COMMENT '0:not group,1:group',
  `Parent` int(11) NOT NULL COMMENT 'parent id-account details id',
  `account_group_level` int(11) NOT NULL COMMENT '1:main group ,0:subgorup',
  `sub_group_level` int(11) NOT NULL COMMENT '0:main group,1:sub group',
  `SystemAccount` int(11) NOT NULL COMMENT 'system genarated not delete',
  `Level` float NOT NULL COMMENT 'level group Eg:1,1.1,1.1.1',
  `narration` varchar(500) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

和这样的数据

    (1, '1000', 'Asset', 1, 0, 1, 0, 1, 0, 'system generated', 1),
(2, '2001', 'Liability', 1, 0, 2, 0, 1, 0, 'system generated', 1),
(3, '3001', 'Income', 1, 0, 3, 0, 1, 0, 'system generated', 1),
(4, '4001', 'Expence', 1, 0, 4, 0, 1, 0, 'system generated', 1),
(5, '1002', 'Calendar', 0, 1, 0, 1, 0, 1, 'assets subaccount', 1),
(6, '1003', 'Contacts', 0, 1, 0, 1, 0, 2, 'assets sub account', 1),
(7, '2002', 'Textbook', 0, 2, 0, 1, 0, 1, 'liability sub account', 1),
(8, '2003', 'Pencil', 0, 2, 0, 1, 0, 2, 'liability sub account', 1),
(9, '2004', 'Notebook', 0, 2, 0, 1, 0, 3, 'liability sub account', 1),
(10, '2006', 'bench', 0, 2, 0, 1, 0, 4, 'liability sub account', 1),
(11, '3002', 'Stamp', 0, 3, 0, 1, 0, 1, 'income sub account', 1),
(12, '3003', 'student fee', 1, 3, 1, 0, 0, 1, 'income sub group', 1),
(13, '3004', 'Tution fee', 0, 12, 0, 1, 0, 1.1, 'income sub group sub account', 1),
(14, '3009', 'bus fee', 1, 12, 0, 1, 0, 1.2, 'income Group-sub account', 1),
(15, '3010', 'Hostel fee', 0, 12, 0, 1, 0, 1.3, 'income group-sub account', 1),
(16, '3012', 'mess fee', 0, 3, 0, 1, 0, 2, 'income sub account', 1),
(17, '4002', 'Note Book', 0, 4, 0, 1, 0, 1, 'expence sub group', 1),
(18, '4003', 'Vehicle parts', 0, 4, 0, 1, 0, 2, 'expence account', 1),
(19, '4007', 'paint', 0, 4, 0, 1, 0, 3, 'expence sub account', 1),
(20, '300942', 'tyre fee', 0, 14, 0, 0, 0, 1.2, 'fsdfs', 1);

像这样account group

的demoimage

我正在尝试

public function grouP_Data() {
        $this->db->select("account_details_id,account_code,account_name,IsGroup,Level");
        $this->db->from("tbl_account_details");
        $this->db->where("status", 1);
        $this->db->where("Level", 0);
        $this->db->where("IsGroup", 1);
        $qur = $this->db->get();
        if ($qur->num_rows() > 0) {
            $this->resul = $qur->result_array();
            for ($i = 0; $i < count($this->resul); $i++) {
                if (!empty($this->resul[$i]["account_details_id"])) {
                    $this->aa = $i;
                    $this->Get_Tree_parent($i, $this->resul[$i]["account_details_id"]);
                }
            }
            return $this->resul;
        } else {
            return false;
        }
    }

    public function Get_Tree_parent($a, $parent) {
        $this->db->select("account_details_id,Parent,account_code,account_name,IsGroup,Level");
        $this->db->from("tbl_account_details");
        $this->db->where("status", 1);
        $this->db->where("Parent", $parent);

        $qure = $this->db->get();
        if ($qure->num_rows() > 0) {
            $res = $qure->result_array();
            if ($this->aa >= 0 && $this->jj == "") {
                $this->resul[$this->aa]["details"] = $res;
            } elseif ($this->aa >= 0 && $this->jj >= 0) {
                $this->resul[$this->aa]["details"][$this->jj]["subdetails"] = $res;
            }
            for ($j = 0; $j < count($res); $j++) {
                if ($res[$j]["IsGroup"] == "1") {
                    $this->jj = $j;
                    $this->Get_Tree_parent($this->aa, $res[$j]["account_details_id"]);
                } else {
                    $this->jj = "";
                }

            }
        }
    }

结果是这样的,

Array
(
    [0] => Array
        (
            [account_details_id] => 1
            [account_code] => 1000
            [account_name] => Asset
            [IsGroup] => 1
            [Level] => 0
            [details] => Array
                (
                    [0] => Array
                        (
                            [account_details_id] => 5
                            [Parent] => 1
                            [account_code] => 1002
                            [account_name] => Calendar
                            [IsGroup] => 0
                            [Level] => 1
                        )

                    [1] => Array
                        (
                            [account_details_id] => 6
                            [Parent] => 1
                            [account_code] => 1003
                            [account_name] => Contacts
                            [IsGroup] => 0
                            [Level] => 2
                        )

                )

        )

    [1] => Array
        (
            [account_details_id] => 2
            [account_code] => 2001
            [account_name] => Liability
            [IsGroup] => 1
            [Level] => 0
            [details] => Array
                (
                    [0] => Array
                        (
                            [account_details_id] => 7
                            [Parent] => 2
                            [account_code] => 2002
                            [account_name] => Textbook
                            [IsGroup] => 0
                            [Level] => 1
                        )

                    [1] => Array
                        (
                            [account_details_id] => 8
                            [Parent] => 2
                            [account_code] => 2003
                            [account_name] => Pencil
                            [IsGroup] => 0
                            [Level] => 2
                        )

                    [2] => Array
                        (
                            [account_details_id] => 9
                            [Parent] => 2
                            [account_code] => 2004
                            [account_name] => Notebook
                            [IsGroup] => 0
                            [Level] => 3
                        )

                    [3] => Array
                        (
                            [account_details_id] => 10
                            [Parent] => 2
                            [account_code] => 2006
                            [account_name] => bench
                            [IsGroup] => 0
                            [Level] => 4
                        )

                )

        )

    [2] => Array
        (
            [account_details_id] => 3
            [account_code] => 3001
            [account_name] => Income
            [IsGroup] => 1
            [Level] => 0
            [details] => Array
                (
                    [0] => Array
                        (
                            [account_details_id] => 11
                            [Parent] => 3
                            [account_code] => 3002
                            [account_name] => Stamp
                            [IsGroup] => 0
                            [Level] => 1
                        )

                    [1] => Array
                        (
                            [account_details_id] => 12
                            [Parent] => 3
                            [account_code] => 3003
                            [account_name] => student fee
                            [IsGroup] => 1
                            [Level] => 1
                            [subdetails] => Array
                                (
                                    [0] => Array
                                        (
                                            [account_details_id] => 20
                                            [Parent] => 14
                                            [account_code] => 300942
                                            [account_name] => tyre fee
                                            [IsGroup] => 0
                                            [Level] => 1.2
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [account_details_id] => 16
                            [Parent] => 3
                            [account_code] => 3012
                            [account_name] => mess fee
                            [IsGroup] => 0
                            [Level] => 2
                        )

                )

        )

    [3] => Array
        (
            [account_details_id] => 4
            [account_code] => 4001
            [account_name] => Expence
            [IsGroup] => 1
            [Level] => 0
            [details] => Array
                (
                    [0] => Array
                        (
                            [account_details_id] => 17
                            [Parent] => 4
                            [account_code] => 4002
                            [account_name] => Note Book
                            [IsGroup] => 0
                            [Level] => 1
                        )

                    [1] => Array
                        (
                            [account_details_id] => 18
                            [Parent] => 4
                            [account_code] => 4003
                            [account_name] => Vehicle parts
                            [IsGroup] => 0
                            [Level] => 2
                        )

                    [2] => Array
                        (
                            [account_details_id] => 19
                            [Parent] => 4
                            [account_code] => 4007
                            [account_name] => paint
                            [IsGroup] => 0
                            [Level] => 3
                        )

                )

        )

)

收入数组下这个方法中没有显示更多的数据和子组。如何解决这个问题?

0 个答案:

没有答案