2012-08-07 33 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;會工作