繁体   English   中英

如何使用PHP动态创建大型下拉菜单?

[英]How to create mega dropdown menu dynamically using PHP?

我需要动态创建巨型下拉菜单。 我已经创建了代码,但无法显示如下: http : //cdn.tutsplus.com/net/uploads/legacy/819_megamenu/demo/index.html

我研究了其他代码,但没有帮助我。

下面是我的代码:

$result = mysql_query("SELECT id, label, link, parent FROM menu ORDER BY parent, sort, label");

//Create a multidimensional array to conatin a list of items and parents
$menu = array(
    'items' => array(),
    'parents' => array()
);

//Builds the array lists with data from the menu table
while($items = mysql_fetch_assoc($result)) {
    //Creates entry into items array with current menu item id ie. $menu['items'][1]
    $menu['items'][$items['id']] = $items;

    //Creates entry into parents array. Parents array contains a list of all items with children
    $menu['parents'][$items['parent']][] = $items['id'];
}
//echo "<pre>"; print_r($menu);

//Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu) {
    $html = "";

    if(isset($menu['parents'][$parent])) {
        $html .= "<ul id='mega-menu-9' class='mega-menu'>";

        foreach($menu['parents'][$parent] as $itemId) {
            if(!isset($menu['parents'][$itemId])) {
                $html .= "<li><a href='Javascript: void(0);' class='arrow'><span><i class='fa fa-home'></i></span>".$menu['items'][$itemId]['label']."</a><li>";
            }

            if(isset($menu['parents'][$itemId])) {
                $html .= "<li><a href='Javascript: void(0);' class='arrow'><span><i class='fa fa-home'></i></span>".$menu['items'][$itemId]['label']."</a>";
                $html .= buildMenu($itemId, $menu);
                $html .= "</li>";
            }
        }

        $html .= "</ul> \n";
    }

    return $html;
}

互联网上有大量的例子。 是一个很好的逐步说明,说明如何构建所需的菜单。

我也建议您寻找jQuery插件,因为那里有很多可行的解决方案。

试试这个,对我有用:)

    $Testresult = mysql_query("select id,anchordescription,RootMenu,link from MenuTable");
$menu = array(
    'menus' => array(),
    'parent_menus' => array()
);
while ($row = mysql_fetch_assoc($Testresult))
{
    $menu['menus'][$row['id']] = $row;
    $menu['parent_menus'][$row['RootMenu']][] = $row['id'];
}

function buildMenu($parent, $menu)
{
    $html = "";

    if (isset($menu['parent_menus'][$parent]))
    {
        $html .= "<ul id='ulNav'>";
        foreach ($menu['parent_menus'][$parent] as $menu_id)
        {
            if (!isset($menu['parent_menus'][$menu_id]))
            {
                $html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['anchordescription'] . "</a></li>";
            }
            if (isset($menu['parent_menus'][$menu_id]))
            {
                $html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['anchordescription'] . "</a>";
                $html .= buildMenu($menu_id, $menu);
                $html .= "</li>";
            }
        }
        $html .= "</ul>";
    }

    return $html;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM