Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用PHP动态创建mega下拉菜单?_Javascript_Php - Fatal编程技术网

Javascript 如何使用PHP动态创建mega下拉菜单?

Javascript 如何使用PHP动态创建mega下拉菜单?,javascript,php,Javascript,Php,我需要动态创建巨型下拉菜单。我已经创建了代码,但无法显示如下内容: 我学习过其他代码,但没有帮助我 下面是我的代码: $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( 'i

我需要动态创建巨型下拉菜单。我已经创建了代码,但无法显示如下内容:

我学习过其他代码,但没有帮助我

下面是我的代码:

$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;
}
$result=mysql\u查询(“按父项、排序、标签从菜单顺序中选择id、标签、链接、父项”);
//创建多维数组以包含项和父项的列表
$menu=数组(
'items'=>array(),
“父项”=>array()
);
//使用菜单表中的数据生成数组列表
while($items=mysql\u fetch\u assoc($result)){
//使用当前菜单项id(即$menu['items'][1])在项目数组中创建条目
$menu['items'][$items['id']]=$items;
//在父数组中创建项。父数组包含所有具有子项的项的列表
$menu['parents'][$items['parent'][]=$items['id'];
}
//回声“;打印(菜单);
//菜单生成器函数,父ID 0为根
函数buildMenu($parent$menu){
$html=“”;
如果(isset($menu['parents'][$parent])){
$html.=“”;
foreach($menu['parents'][$parent]作为$itemId){
如果(!isset($menu['parents'][$itemId])){
$html.=“
  • ”; } 如果(isset($menu['parents'][$itemId])){ $html.=“
  • ”; $html.=buildMenu($itemId,$menu); $html.=“
  • ”; } } $html.=“\n”; } 返回$html; }
    互联网上有大量的例子。是一个很好的一步一步的解释如何建立一个菜单像一个你想要的


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

    试试这个,它对我有用:)

    $Testresult=mysql\u查询(“选择id、主播描述、根菜单、菜单表链接”);
    $menu=数组(
    “菜单”=>array(),
    “父菜单”=>array()
    );
    while($row=mysql\u fetch\u assoc($Testresult))
    {
    $menu['menus'][$row['id']]=$row;
    $menu['parent_menus'][$row['RootMenu']][]=$row['id'];
    }
    函数buildMenu($parent$menu)
    {
    $html=“”;
    如果(isset($menu['parent_menus'][$parent]))
    {
    $html.=“”;
    foreach($menu['parent\u menus'][$parent]作为$menu\u id)
    {
    如果(!isset($menu['parent\u menu'][$menu\u id]))
    {
    $html.=“
  • ”; } 如果(设置($menu['parent\u menus'][$menu\u id])) { $html.=“
  • ”; $html.=buildMenu($menu\u id,$menu); $html.=“
  • ”; } } $html.=“”; } 返回$html; }
    感谢您的快速响应,但我需要使用php和mysql动态地进行响应。我有一个表格,可以保存所有菜单及其父菜单,但不能像使用递归函数打印的超级菜单一样打印。那么,您到底有什么问题?你的代码有什么问题吗?很抱歉,回复太晚,是的,它不会像我提供的url那样创建mega下拉菜单,我需要完全按照给定的url、多列和多级mega下拉菜单创建它,以便将相关类添加到与你提供的示例相同的相关元素中。
        $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;
    }