我在这上面花了好几个小时, 并且可以使用第二双眼睛。 我使用 Magento, 想要打印一个文本等级。 比如 :
Default Category > Weightlifting Bars & Plates > Bars
我的文字是重复的,这就是我崩溃的表情
category_name (top_category, sub_category1, sub_sub_category1, sub_sub_sub_category3, etc.)
这就是我有的:
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)
注意它开始重复 在“ 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());
Pay no attention to $print_category. It is a variable I m using for some other use. I know its a logic error, not a Magento error. The code works fine.
感谢您的帮助 谢谢