2012-08-07 75 views
0

我必须从数据库中获取主类别,子级,子级等。得到了主类和子类,但是没有得到foreach循环外的subchild。在循环内部获取了正确的值。我为此使用了下面的代码...我已经在$ children_data = array()后面声明了$ sub_childs = array(),但是后来我在browswer中获得了空白页面。任何人都请帮忙。感谢ü数组值没有得到外部foreach循环

 $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']) 
       ); 
      } 
     } 

回答

1

似乎是一个范围问题,请尝试首先初始化的foreach以外的变量:

$sub_childs = array(); 

foreach ($children as $child) { 

也许只是$sub_childs;会工作