0

我必须从 db 中获取主要类别、子类别、子类别等。获得了主要类别和子类别,但没有在 foreach 循环之外获得子类别。在循环内部获得了正确的值。我正在为此使用以下代码...我已经在 $children_data = array() 之后声明了 $sub_childs = array() 但后来我在浏览器中得到了空白页.. 请大家帮忙。感谢你

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');

        $this->data['categories'] = array();

        $categories = $this->model_catalog_category->getCategories(0);

        foreach ($categories as $category) {
            if ($category['top']) {$sub_childs = array();
                $children_data = array();

                $children = $this->model_catalog_category->getCategories($category['category_id']);

                foreach ($children as $child) {

                    $sub_childs = array();
                    $sub_child = $this->model_catalog_category->getCategories($child['category_id']);
                    // echo '<pre>'; print_r($children_child);// got the correct values in here
                    foreach ($sub_child as $c_child) {
                        $data = array(
                            'filter_category_id' => $c_child['category_id'],
                            'filter_sub_category' => true
                        );

                        if ($this->config->get('config_product_count')) {
                            $product_total = $this->model_catalog_product->getTotalProducts($data);
                            $c_child['name'] .= ' (' . $product_total . ')';
                        }

                        $sub_childs[] = array(
                            'name' => $c_child['name'],
                            'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $c_child['category_id'])
                        );
                    }
                    //echo '<pre>'; print_r($sub_childs);// here also got it
                    $data = array(
                        'filter_category_id' => $child['category_id'],
                        'filter_sub_category' => true
                    );

                    if ($this->config->get('config_product_count')) {
                        $product_total = $this->model_catalog_product->getTotalProducts($data);
                        $child['name'] .= ' (' . $product_total . ')';
                    }

                    $children_data[] = array(
                        'name' => $child['name'],
                        'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                    );
                    echo '<pre>'; print_r($sub_childs);
                }
                //echo '<pre>'; print_r($sub_childs);
                // Level 1
                $this->data['categories'][] = array(
                    'name' => $category['name'],
                    'children' => $children_data,
                    'child' => $sub_childs,                  // didn't get here 
                    'column' => $category['column'] ? $category['column'] : 1,
                    'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
                );
            }
        }
4

1 回答 1

1

似乎是范围问题,请先尝试在 foreach 之外初始化变量:

$sub_childs = array();

foreach ($children as $child) {

可能只是$sub_childs;会工作

于 2012-08-07T12:13:32.167 回答