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 に答える