构建递归导航菜单php

时间:2014-05-02 04:40:16

标签: php recursion menubar

要构建动态导航菜单,我运行了查询

select `category_id`,`category_name`,`parent_category_id`,`sort_order` from `item_category` 

我得到了结果

Array
(
    [0] => Array
        (
            [category_id] => 9
            [category_name] => cat1
            [parent_category_id] => 0
            [sort_order] => 0
        )

    [1] => Array
        (
            [category_id] => 11
            [category_name] => cat3
            [parent_category_id] => 0
            [sort_order] => 0
        )

    [2] => Array
        (
            [category_id] => 15
            [category_name] => cat7
            [parent_category_id] => 10
            [sort_order] => 0
        )

    [3] => Array
        (
            [category_id] => 17
            [category_name] => cat9
            [parent_category_id] => 10
            [sort_order] => 1
        )

    [4] => Array
        (
            [category_id] => 13
            [category_name] => cat5
            [parent_category_id] => 11
            [sort_order] => 0
        )

    [5] => Array
        (
            [category_id] => 16
            [category_name] => cat8
            [parent_category_id] => 15
            [sort_order] => 0
        )

    [6] => Array
        (
            [category_id] => 10
            [category_name] => cat2
            [parent_category_id] => 9
            [sort_order] => 1
        )

    [7] => Array
        (
            [category_id] => 12
            [category_name] => test5
            [parent_category_id] => 9
            [sort_order] => 0
        )

)

我试图通过

构建导航栏
function create_array($number, $data)
{
    $result = array();
    foreach ($data as $row)
    {
        if ($row['parent_category_id'] == $number)
        {
            $result[$row['category_id']] = create_array($row['category_id'], $data);

        }
    }
    return $result;
}
$menu_array=create_array(0, $all_categories);
echo "menu_array<pre>"; print_r($menu_array); echo "</pre>";

它完美地构建了这样的菜单

Array
(
    [9] => Array
        (
            [10] => Array
                (
                    [15] => Array
                        (
                            [16] => Array
                                (
                                )

                        )

                    [17] => Array
                        (
                        )

                )

            [12] => Array
                (
                )

        )

    [11] => Array
        (
            [13] => Array
                (
                )

        )

)

我现在的问题是我不明白如何使用create_arraysort_order函数中进行排序,还检索如何从category_name检索create_array parent_category_id函数递归。

修改 谢谢@ user3091574由于某些其他原因,我之前按category_name排序,现在为了简单地提出您的建议,现在我想要检索<ul>并构建{{1}}!有什么建议?

修改-2 我在https://stackoverflow.com/a/3380296/1528701

找到了构建导航菜单的解决方案

1 个答案:

答案 0 :(得分:2)

进行排序:

在查询中按sort_order DESC添加订单

添加catogary

function create_array($number, $data)
{
    $result = array();

    foreach ($data as $row)
    {
        if ($row['parent_category_id'] == $number)
        {
            $result[$row['category_id']]['category_name']=$row['category_name'];
            $result[$row['category_id']]['id']= create_array($row['category_id'], $data);

        }
    }
    return $result;
}