atwright147
atwright147

Reputation: 3821

CodeIgniter heirarchical loop with multidimensional array

I have am generating the array at the bottom of this message. I am using CodeIgniter and this array to display heirarchical information via the following view:

<?php foreach($products['top_cats'] as $top_cat): // $top_cat ?>
    <h2><?php echo $top_cat['cat_name']; ?></h2>
    <?php if( ! empty($products['sub_cats'][$top_cat['cat_id']])): ?>
        <?php foreach($products['sub_cats'][$top_cat['cat_id']] as $sub_cat): ?>
            <h3><?php echo $sub_cat['name']; ?></h3>
            <!-- problem below -->
            <?php $this->load->view('products_table_v', $products[$sub_cat['id']][$n]); ?>
            <?php var_dump($n); ?>
        <?php endforeach; ?>
    <?php endif; ?>
<?php endforeach; ?>

$products[top_cats][14] relates to $products[sub_cats][14] and $products[sub_cats][14] relates to $products[products][14] (I hope this makes sense). So I need to pass $products[products][14] to the view when I am in the foreach for $products[sub_cats][14] (again, I hope this makes sense?)...

My problem is that I need to pass the correct sub-sub(-sub) array to the sub-view (which contains a good working foreach loop). I cannot get my head around how to give the array the the sub-view (I have gone code blind).

Please help.

Array
(
    [top_cats] =&gt; Array
        (
            [14] =&gt; Array
                (
                    [cat_id] =&gt; 14
                    [cat_name] =&gt; Alcopops
                    [cat_desc] =&gt; 
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

            [2] =&gt; Array
                (
                    [cat_id] =&gt; 2
                    [cat_name] =&gt; Beers and Lagers
                    [cat_desc] =&gt; <p>hh</p>
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 0
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

            [79] =&gt; Array
                (
                    [cat_id] =&gt; 79
                    [cat_name] =&gt; Household
                    [cat_desc] =&gt; 
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

            [5] =&gt; Array
                (
                    [cat_id] =&gt; 5
                    [cat_name] =&gt; Soft Drinks
                    [cat_desc] =&gt; 
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

            [4] =&gt; Array
                (
                    [cat_id] =&gt; 4
                    [cat_name] =&gt; Spirits
                    [cat_desc] =&gt; 
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

            [3] =&gt; Array
                (
                    [cat_id] =&gt; 3
                    [cat_name] =&gt; Wines
                    [cat_desc] =&gt; 
                    [cat_featured] =&gt; n
                    [cat_parent_id] =&gt; 
                    [cat_sort_order] =&gt; 
                    [category_active] =&gt; y
                )

        )

    [sub_cats] =&gt; Array
        (
            [3] =&gt; Array
                (
                    [25] =&gt; Array
                        (
                            [id] =&gt; 25
                            [name] =&gt; Champagne and Sparkling Wines
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 3
                        )

                    [22] =&gt; Array
                        (
                            [id] =&gt; 22
                            [name] =&gt; Australian Wines
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 3
                        )


                )

            [14] =&gt; Array
                (
                    [30] =&gt; Array
                        (
                            [id] =&gt; 30
                            [name] =&gt; White Rum
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 4
                        )

                    [32] =&gt; Array
                        (
                            [id] =&gt; 32
                            [name] =&gt; Liquers
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 4
                        )

                )

            [2] =&gt; Array
                (
                    [8] =&gt; Array
                        (
                            [id] =&gt; 8
                            [name] =&gt; Canned Lager
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                    [12] =&gt; Array
                        (
                            [id] =&gt; 12
                            [name] =&gt; Bottled Beer
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                    [10] =&gt; Array
                        (
                            [id] =&gt; 10
                            [name] =&gt; Bitter Cans
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                    [7] =&gt; Array
                        (
                            [id] =&gt; 7
                            [name] =&gt; Super Beers
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                    [13] =&gt; Array
                        (
                            [id] =&gt; 13
                            [name] =&gt; Cider
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                    [9] =&gt; Array
                        (
                            [id] =&gt; 9
                            [name] =&gt; Stout
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 2
                        )

                )

            [] =&gt; Array
                (
                    [5] =&gt; Array
                        (
                            [id] =&gt; 5
                            [name] =&gt; Soft Drinks
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 
                        )

                    [79] =&gt; Array
                        (
                            [id] =&gt; 79
                            [name] =&gt; Household
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 
                        )

                    [14] =&gt; Array
                        (
                            [id] =&gt; 14
                            [name] =&gt; Alcopops
                            [desc] =&gt; 
                            [cat_parent_id] =&gt; 
                        )

                )

        )

    [products] =&gt; Array
        (
            [25] =&gt; Array
                (
                    [1347] =&gt; Array
                        (
                            [product_id] =&gt; 1347
                            [category_id] =&gt; 25
                            [name] =&gt; Asti Spumante Martini
                            [description] =&gt; 
                            [source_price] =&gt; 29.00
                            [source_pack_size] =&gt; 6
                            [source_item_size] =&gt; 75.00
                            [source_unit_id] =&gt; 2
                            [resale_price] =&gt; 7.00
                            [resale_pack_size] =&gt; 1
                            [percentage_return] =&gt; 22.59
                            [product_code] =&gt; WNAS001
                            [product_image] =&gt; 1347_.jpg
                            [current_stock] =&gt; 5
                            [stock_level_id] =&gt; 0
                            [featured] =&gt; y
                            [active] =&gt; y
                            [so_member_of_group_id] =&gt; 0
                            [discount_amount] =&gt; 3.50
                            [created_at] =&gt; 2009-07-22 10:21:57
                            [modified_at] =&gt; 
                            [product_unit_id] =&gt; 2
                            [unit_name] =&gt; Centilitre
                            [unit_symbol] =&gt; cl
                            [stock_levels_id] =&gt; 
                            [stock_levels_name] =&gt; 
                            [cat_id] =&gt; 25
                            [cat_name] =&gt; Champagne and Sparkling Wines
                            [cat_desc] =&gt; 
                            [cat_featured] =&gt; n
                            [cat_parent_id] =&gt; 3
                            [cat_sort_order] =&gt; 
                            [category_active] =&gt; y
                        )

                    [1566] =&gt; Array
                        (
                            [product_id] =&gt; 1566
                            [category_id] =&gt; 25
                            [name] =&gt; Blossom Hill Zinfandel Sparkling
                            [description] =&gt; 
                            [source_price] =&gt; 29.99
                            [source_pack_size] =&gt; 6
                            [source_item_size] =&gt; 70.00
                            [source_unit_id] =&gt; 2
                            [resale_price] =&gt; 8.49
                            [resale_pack_size] =&gt; 1
                            [percentage_return] =&gt; 29.35
                            [product_code] =&gt; WNBH010
                            [product_image] =&gt; 
                            [current_stock] =&gt; 30
                            [stock_level_id] =&gt; 
                            [featured] =&gt; n
                            [active] =&gt; y
                            [so_member_of_group_id] =&gt; 
                            [discount_amount] =&gt; 0.00
                            [created_at] =&gt; 2011-03-21 10:35:47
                            [modified_at] =&gt; 
                            [product_unit_id] =&gt; 2
                            [unit_name] =&gt; Centilitre
                            [unit_symbol] =&gt; cl
                            [stock_levels_id] =&gt; 
                            [stock_levels_name] =&gt; 
                            [cat_id] =&gt; 25
                            [cat_name] =&gt; Champagne and Sparkling Wines
                            [cat_desc] =&gt; 
                            [cat_featured] =&gt; n
                            [cat_parent_id] =&gt; 3
                            [cat_sort_order] =&gt; 
                            [category_active] =&gt; y
                        )


                )

            [22] =&gt; Array
                (
                    [1020] =&gt; Array
                        (
                            [product_id] =&gt; 1020
                            [category_id] =&gt; 22
                            [name] =&gt; Auction House Cabernet Sauvignon Merlot (Aus)
                            [description] =&gt; 
                            [source_price] =&gt; 20.00
                            [source_pack_size] =&gt; 6
                            [source_item_size] =&gt; 75.00
                            [source_unit_id] =&gt; 2
                            [resale_price] =&gt; 5.00
                            [resale_pack_size] =&gt; 1
                            [percentage_return] =&gt; 27.18
                            [product_code] =&gt; WNAH008
                            [product_image] =&gt; 1020_.jpg
                            [current_stock] =&gt; 35
                            [stock_level_id] =&gt; 0
                            [featured] =&gt; y
                            [active] =&gt; y
                            [so_member_of_group_id] =&gt; 0
                            [discount_amount] =&gt; 0.75
                            [created_at] =&gt; 2008-03-10 14:56:16
                            [modified_at] =&gt; 
                            [product_unit_id] =&gt; 2
                            [unit_name] =&gt; Centilitre
                            [unit_symbol] =&gt; cl
                            [stock_levels_id] =&gt; 
                            [stock_levels_name] =&gt; 
                            [cat_id] =&gt; 22
                            [cat_name] =&gt; Australian Wines
                            [cat_desc] =&gt; 
                            [cat_featured] =&gt; n
                            [cat_parent_id] =&gt; 3
                            [cat_sort_order] =&gt; 
                            [category_active] =&gt; y
                        )

                )

        )

)

Upvotes: 0

Views: 716

Answers (1)

Zombaya
Zombaya

Reputation: 2258

foreach($products['top_cats'] as $top_cat): // $top_cat ?>
    <h2><?php echo $top_cat['cat_name']; ?></h2>
    <?php if( ! empty($products['sub_cats'][$top_cat['cat_id']])): ?>
        <?php foreach($products['sub_cats'][$top_cat['cat_id']] as $sub_cat): ?>
            <h3><?php echo $sub_cat['name']; ?></h3>

            // Replaced bit
            <?php if( ! empty($products['products'][$sub_cat['id']])): ?>
                <?php $this->load->view('products_table_v', $products['products'][$sub_cat['id']]); ?>
            <?php endif; ?>

        <?php endforeach; ?>
    <?php endif; ?>
<?php endforeach; ?>

Upvotes: 1

Related Questions