0

我在这条消息的底部生成了数组。我正在使用 CodeIgniter 和此数组通过以下视图显示层次结构信息:

<?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]涉及$products[sub_cats][14]$products[sub_cats][14]涉及$products[products][14](我希望这是有道理的)。$products[products][14]因此,当我在 foreach for 中时,我需要传递给视图$products[sub_cats][14](再次,我希望这是有道理的?)...

我的问题是我需要将正确的 sub-sub(-sub) 数组传递给子视图(其中包含一个良好的工作 foreach 循环)。我无法理解如何为数组提供子视图(我已经把代码弄瞎了)。

请帮忙。

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
                        )

                )

        )

)
4

1 回答 1

1
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; ?>
于 2012-05-08T22:11:24.743 回答