简体   繁体   中英

How to generate category tree in codeigniter?`

hello all i want to generate category tree like wordpress of n levels category.

Here is my example array.

$arreay = Array
    (
        [0] => stdClass Object
            (
                [Id] => 12877
                [CoaName] => Ordinary Income/Expense
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 0
                [flgActive] => 1
            )

        [1] => stdClass Object
            (
                [Id] => 12878
                [CoaName] => Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 1
                [flgActive] => 1
            )

        [2] => stdClass Object
            (
                [CoaName] => I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 23
                [flgActive] => 1
            )

        [3] => stdClass Object
            (
                [CoaName] => 400000 · ROOM REVENUE
                [ParentCat] => 23
                [Auto] => 24
                [flgActive] => 1
            )

        [4] => stdClass Object
            (
                [CoaName] => 410101 · Net Room Revenue Taxable
                [ParentCat] => 24
                [Auto] => 27
                [flgActive] => 1
            )

        [5] => stdClass Object
            (
                [CoaName] => 415101 · GTD No Show
                [ParentCat] => 24
                [Auto] => 32
                [flgActive] => 1
            )

        [6] => stdClass Object
            (
                [CoaName] => Total 400000 · ROOM REVENUE
                [ParentCat] => 23
                [Auto] => 35
                [flgActive] => 1
            )

        [7] => stdClass Object
            (
                [CoaName] => Total I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 37
                [flgActive] => 1
            )

        [8] => stdClass Object
            (
                [CoaName] => II · OTHER OPERATING REVENUE
                [ParentCat] => 1
                [Auto] => 38
                [flgActive] => 1
            )

        [9] => stdClass Object
            (
                [CoaName] => 480000 · OTHER INCOME
                [ParentCat] => 38
                [Auto] => 39
                [flgActive] => 1
            )

        [10] => stdClass Object
            (
                [CoaName] => 480380 · Interest Income
                [ParentCat] => 39
                [Auto] => 49
                [flgActive] => 1
            )

        [11] => stdClass Object
            (
                [CoaName] => 480383 · Guest Laundry
                [ParentCat] => 39
                [Auto] => 51
                [flgActive] => 1
            )

        [12] => stdClass Object
            (
                [CoaName] => 480385 · Vending
                [ParentCat] => 39
                [Auto] => 53
                [flgActive] => 1
            )

        [13] => stdClass Object
            (
                [CoaName] => 480389 · Miscellaneous
                [ParentCat] => 39
                [Auto] => 54
                [flgActive] => 1
            )

        [14] => stdClass Object
            (
                [CoaName] => 480390 · Pet Fees
                [ParentCat] => 39
                [Auto] => 55
                [flgActive] => 1
            )

        [15] => stdClass Object
            (
                [CoaName] => 482000 · Sales Tax Discounts
                [ParentCat] => 39
                [Auto] => 70
                [flgActive] => 1
            )

        [16] => stdClass Object
            (
                [CoaName] => Total 480000 · OTHER INCOME
                [ParentCat] => 38
                [Auto] => 75
                [flgActive] => 1
            )

        [17] => stdClass Object
            (
                [CoaName] => Total II · OTHER OPERATING REVENUE
                [ParentCat] => 1
                [Auto] => 77
                [flgActive] => 1
            )

        [18] => stdClass Object
            (
                [Id] => 12955
                [CoaName] => Total Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 78
                [flgActive] => 1
            )

        [19] => stdClass Object
            (
                [Id] => 12956
                [CoaName] => Cost of Goods Sold
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 79
                [flgActive] => 1
            )

        [20] => stdClass Object
            (
                [CoaName] => COST OF OTHER OPERATING SALES
                [ParentCat] => 79
                [Auto] => 80
                [flgActive] => 1
            )

        [21] => stdClass Object
            (
                [Id] => 12968
                [CoaName] => Gross Profit
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 91
                [flgActive] => 1
            )

        [22] => stdClass Object
            (
                [Id] => 12969
                [CoaName] => Expense
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 92
                [flgActive] => 1
            )

        [23] => stdClass Object
            (
                [CoaName] => 10 · ROOMS DEPARTMENT
                [ParentCat] => 92
                [Auto] => 98
                [flgActive] => 1
            )

        [24] => stdClass Object
            (
                [CoaName] => 1015 · SALARY WAGES & BONUS
                [ParentCat] => 98
                [Auto] => 99
                [flgActive] => 1
            )

        [25] => stdClass Object
            (
                [CoaName] => 101501 · Front Desk
                [ParentCat] => 99
                [Auto] => 101
                [flgActive] => 1
            )

        [26] => stdClass Object
            (
                [CoaName] => 101503 · Housekeepers
                [ParentCat] => 99
                [Auto] => 103
                [flgActive] => 1
            )

        [27] => stdClass Object
            (
                [CoaName] => 101504 · Laundry
                [ParentCat] => 99
                [Auto] => 104
                [flgActive] => 1
            )

        [28] => stdClass Object
            (
                [CoaName] => 101505 · Hostess
                [ParentCat] => 99
                [Auto] => 105
                [flgActive] => 1
            )

        [29] => stdClass Object
            (
                [CoaName] => 101506 · Contract Labor
                [ParentCat] => 99
                [Auto] => 106
                [flgActive] => 1
            )

        [30] => stdClass Object
            (
                [CoaName] => 101507 · Bonuses & Incentives
                [ParentCat] => 99
                [Auto] => 107
                [flgActive] => 1
            )

        [31] => stdClass Object
            (
                [CoaName] => 101508 · Housekeeping Supervisor
                [ParentCat] => 99
                [Auto] => 108
                [flgActive] => 1
            )

        [32] => stdClass Object
            (
                [CoaName] => 6002 · SALARIES AND WAGES
                [ParentCat] => 99
                [Auto] => 109
                [flgActive] => 1
            )

        [33] => stdClass ObjectI had a 800 elements in a array now i am trying to generates its paren
            (
                [CoaName] => Total 1015 · SALARY WAGES & BONUS
                [ParentCat] => 98
                [Auto] => 116
                [flgActive] => 1
            )

        [34] => stdClass Object
            (
                [CoaName] => 1020 · PAYROLL RELATED EXPENSE
                [ParentCat] => 98
                [Auto] => 117
                [flgActive] => 1
            )

        [35] => stdClass Object
            (
                [CoaName] => 102001 · Employee Medical
                [ParentCat] => 117
                [Auto] => 118
                [flgActive] => 1
            )

        [36] => stdClass Object
            (
                [CoaName] => 102003 · Sick
                [ParentCat] => 117
                [Auto] => 120
                [flgActive] => 1
            )

        [37] => stdClass Object
            (
                [CoaName] => 102004 · Vacation
                [ParentCat] => 117
                [Auto] => 121
                [flgActive] => 1
            )

        [38] => stdClass Object
            (
                [CoaName] => 102005 · WORKERS COMP
                [ParentCat] => 117
                [Auto] => 122
                [flgActive] => 1
            )

        [39] => stdClass Object
            (
                [CoaName] => Total 1020 · PAYROLL RELATED EXPENSE
                [ParentCat] => 98
                [Auto] => 141
                [flgActive] => 1
            )

        [40] => stdClass Object
            (
                [CoaName] => 1030 · ROOM SUPPLIES
                [ParentCat] => 98
                [Auto] => 142
                [flgActive] => 1
            )

now parent category and child category relation is between ParentCat field and auto field.

Now i had try this code

foreach($tree as $el){
            $thisref = &$refs->{$el->Auto};
                foreach($el as $k => $v){
                @$thisref->{$k} = $v;
            }
            if($el->ParentCat == 0){
                $reportTree->{$el->Auto} = &$thisref;
            } 
            else{
                $refs->{$el->ParentCat}->children->{$el->Auto} = &$thisref;
            }
            $reportTree;
        }

i have 800 elements in a array and try to generate tree with above code. But single some elements insert in array 100 times. But it should insert one time only.

Parent Child relation For example Here i am taking 2 elements from array.

[1] => stdClass Object
            (
                [Id] => 12878
                [CoaName] => Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 1
                [flgActive] => 1
            )

        [2] => stdClass Object
            (
                [CoaName] => I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 23
                [flgActive] => 1
            )

Now 2nd element has ParentCat value 1 and first element has Auto 1 so first element is parent of second element. So can you Please suggest me how to do it. Thanks

If i understand you correctly - the following should work:

First of all assign the array by your id Keys (according to your php example there isn't always an id - but i assume this is a copy error)

$arrTreeById = array();
$arrTree = array();

foreach($arrTree AS $objItem)
{
    $arrTreeById[$objItem->Id] = $objItem;
    $objItem->arrChilds = array();
}

and after that assignment you simply add the childs to the parents

foreach($arrTreeById AS $objItem)
{
    if (isset($arrTree[$objItem->ParentCat]))   $arrTree[$objItem->ParentCat]->arrChilds[] = $objItem;
    if ($objItem->ParentCat == 0) $intStartingKey = $objItem->Id;
}

print_r($arrTree[$intStartingKey]);

or am i missing something here ?

I have develop a function for this just pass array to this function

function buildArrayTree($items){
        $ref = array(); // track references
        $Childrens = array(); // new array

        // menu reference since can't push directly later
        foreach($original = &$items as $item)
        {
            if($item->ParentCat == 0)
            {
                // add item
                $Childrens[$item->Auto] = array($item);

                // track item reference
                $ref[$item->Auto] = &$Childrens[$item->Auto];
            }
            else
            {
                // index of Childrens array
                $count = isset($ref[$item->ParentCat]['Childrens'])
                    ? count($ref[$item->ParentCat]['Childrens']) : 0;

                // parent already there? else move to back
                if(isset($ref[$item->ParentCat]))
                {
                    // set item under proper Childrens array item
                    $ref[$item->ParentCat]['Childrens'][] = array($item);

                    // track reference to new item
                    $ref[$item->Auto] = &$ref[$item->ParentCat]['Childrens'][$count];
                }
                else
                {
                    // if parent isn't there, add to end of array and we'll get to it later
                    array_push($original, $item);
                }
            }
        }
        return $Childrens;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM