我必须从db获得主要类别,子级,子级等。得到了主类和子类,但没有得到foreach循环之外的子代。在循环内部获取正确的值。我正在使用以下代码...我已经在$ children_data = array()之后声明了$ sub_childs = array()但是我在browswer中得到了空白页面...任何人都请帮助。谢谢你
$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'])
);
}
}
答案 0 :(得分:1)
看起来像范围问题,请先尝试在foreach之外初始化变量:
$sub_childs = array();
foreach ($children as $child) {
可能只有$sub_childs;
才能正常工作