我在这上面花了好几个小时,可以用第二双眼睛。我正在使用 Magento 并想打印类别的“文本层次结构”。就像是:
Default Category > Weightlifting Bars & Plates > Bars
我的文字是重复的。这是我的故障应该是什么样子
类别名称(top_category、sub_category1、sub_sub_category1、sub_sub_sub_category3 等)
这是我所拥有的:
Default Category (Default Category)
Weightlifting Bars & Plates (Default Category > Weightlifting Bars & Plates)
Bars (Default Category > Weightlifting Bars & Plates > Bars)
Men's 20kg Bars (Default Category > Weightlifting Bars & Plates > Bars > Men's 20kg Bars)
Women's 15kg Bars (Default Category > Weightlifting Bars & Plates > Bars > Men's 20kg Bars > Women's 15kg Bars)
Junior 10k Bars (Default Category > Weightlifting Bars & Plates > Bars > Men's 20kg Bars > Women's 15kg Bars > Junior 10k Bars)
Technique Bars (Default Category > Weightlifting Bars & Plates > Bars > Men's 20kg Bars > Women's 15kg Bars > Junior 10k Bars > Technique Bars)
请注意,它在“Men's 20kg Bars”之后开始重复。那是因为变量没有被重置。
这是我的代码:
function print_tree($tree, $level, $category, $root_id, $print_category) {
$r = $root_id;
$cat = $category;
$level++;
foreach($tree as $item) {
if ( $item['name'] == "Default Category" || $r != '' ) {
if ($level == 1) {
$cat = $item['name'];
}
else {
$cat .= " > " . $item['name'];
}
$parent_id = ($r != '' ? $r : $item['category_id']);
echo str_repeat(" ", $level) . $item['name'] . " (" . $cat . ")<br/>";
$this->print_tree(
$item['children'],
$level,
$cat,
$parent_id,
$print_category
);
}
}
return;
}
什么叫它:
$string = Mage::helper('googlemerchant/data')->print_tree($tree['children'], 0, 'Default Category', 0, $cat->getName());
不要关注 $print_category。这是我用于其他用途的变量。我知道这是一个逻辑错误,而不是 Magento 错误。代码工作正常。
感谢帮助。谢谢你。