0

刚刚让这段代码适用于我的菜单的 2 个级别。但我希望它可以无限级别地工作。你们中有人知道从哪里开始吗?现在,如果我在数据库中输入更多级别,它会显示“未定义索引链接和标签行 29”,并且已输入的新父级不会显示。

$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
    if ($obj->parent_id == 0) {
        $parent_menu[$obj->id]['label'] = $obj->label;
        $parent_menu[$obj->id]['link'] = $obj->link_url;
    } else {
        $sub_menu[$obj->id]['parent'] = $obj->parent_id;
        $sub_menu[$obj->id]['label'] = $obj->label;
        $sub_menu[$obj->id]['link'] = $obj->link_url;
        if (!isset($parent_menu[$obj->parent_id]['count'])) {
            $parent_menu[$obj->parent_id]['count'] = 0;
        }
        $parent_menu[$obj->parent_id]['count']++;
    }
}
mysql_free_result($items);

function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
    $menu = "<ul id=\"".$main_id."\">\n";
    foreach ($parent_array as $pkey => $pval) {
        if (!empty($pval['count'])) {
            $menu .= "  <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
        } else {
            $menu .= "  <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li>\n";
        }
        if (!empty($_REQUEST[$qs_val])) {
            $menu .= "<ul id=\"".$sub_id."\">\n";
            foreach ($sub_array as $sval) {
                if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
                    $menu .= "<li><a href=\"".rebuild_link($sval['link'], $qs_val, $sval['parent'])."\" target=\"_blank\">".$sval['label']."</a></li>\n";
                }
            }
            $menu .= "</ul>\n";
        }
    }

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

function rebuild_link($link, $parent_var, $parent_val) {
    $link_parts = explode("?", $link);
    $base_var = "?".$parent_var."=".$parent_val;
    if (!empty($link_parts[1])) {
        $link_parts[1] = str_replace("&amp;", "##", $link_parts[1]);
        $parts = explode("##", $link_parts[1]);
        $newParts = array();
        foreach ($parts as $val) {
            $val_parts = explode("=", $val);
            if ($val_parts[0] != $parent_var) {
                array_push($newParts, $val);
            }
        }
        if (count($newParts) != 0) {
            $qs = "&amp;".implode("&amp;", $newParts);
        }
        return $link_parts[0].$base_var.$qs;
    } else {
        return $link_parts[0].$base_var;
    }
}

echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
4

1 回答 1

0

为此,您需要使用递归函数来处理您的数组 -> 列表转换。

这是一个例子:

<?php
function arrToUl($arr) {
    $out = '<ul>';
    foreach($arr as $key=>$val) {
        $out .= '<li>';
        $out .= is_array($val) ? arrToUl($val) : $val;
        $out .= '</li>';
    }
    return $out . '</ul>';
}

$arr = array('firstval','secondval',array('firstval2','secondval2',array('firstval3','secondval3',array('firstval4','secondval4',array('firstval5','secondval5')))));
echo arrToUl($arr);

jsfiddle中输出。

于 2012-10-18T16:04:56.940 回答