English 中文(简体)
基因和再生
原标题:Magento and recursion

我在这上面花了好几个小时, 并且可以使用第二双眼睛。 我使用 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("&nbsp;&nbsp;&nbsp;&nbsp;", $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.

感谢您的帮助 谢谢

最佳回答

我想通了,我必须修新猫(以前是猫)

    function print_tree($tree, $level, $category, $root_id, $print_category) {
    $r = $root_id;
    $old_cat = $category;
    $new_cat = $category;
    $level++;

    foreach($tree as $item) {
        if ( $item[ name ] == "Default Category" || $r !=    ) {
            if ($level == 1) {
                $new_cat = $item[ name ];
            }
            else {
                $new_cat = $old_cat . " &gt; " . $item[ name ];
            }

            $parent_id = ($r !=    ? $r : $item[ category_id ]);

            if ( count($item[ children ]) != 0 ) {
                echo "level " . $level . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level) . $item[ name ] . " (" . $old_cat . ") <br/>";
                $this->print_tree(
                    $item[ children ],
                    $level,
                    $new_cat,
                    $parent_id,
                    $print_category
                );
            }
            else {
                echo "level " . $level . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level) . $item[ name ] . " (" . $old_cat . ")<br/>";
                $this->print_tree(
                    $item[ children ],
                    $level,
                    $old_cat,
                    $parent_id,
                    $print_category
                );
            }
        }
    }

    return;
}
问题回答

暂无回答




相关问题
Recursive same-table query in SQL Server 2008

I have the following table in a SQL Server 2008 database: Id Name ParentFolder -- ---- ------------ 1 Europe NULL 2 Asia NULL 3 Germany 1 4 UK 1 5 China ...

Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签