PHP动态下拉菜单 [英] PHP Dynamic Drop Down Menu

查看:316
本文介绍了PHP动态下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下: 我有一个菜单,需要从数据库中动态创建. 菜单层次结构由表中的父"列确定(每个条目只有一个父项,如果只有一个父项,则为NULL)

Here's the situation: I have a menu that needs to be created dynamically from the database. The menu hierarchy is determined by a 'parent' column in the table (each entry has one parent or NULL if it is only a parent)

问题是,考虑到下拉菜单需要正确的<ul><li><ul><li>结构,我无法考虑如何动态地执行此操作. 这就要求我在父页面的foreach中有我的子页面的"foreach"吗? 如果这有意义,有解决方案吗?

The problem is that I can't think of how I would dynamically do this, considering I need proper <ul><li><ul><li> structure for my drop-down menu. This requires that I have my 'foreach' of child pages, within the foreach of parent pages? If that makes sense, is there a solution?

仅供参考:我正在使用的数组返回:

FYI: The array I am working with returns:

array(31) { 
[0]=>  array(5)
     { ["id"]=>  string(2) "31" ["title"]=>  string(4) "Home" ["linkable"]=>  string(1) "1" ["parent"]=>  NULL ["override"]=>  string(1) " " } 
[1]=>  array(5)
     { ["id"]=>  string(2) "30" ["title"]=>  string(11) "Shop Online" ["linkable"]=>  string(1) "1" ["parent"]=> string(2) "31" ["override"]=>  string(4) "shop" } 

and on and on.

推荐答案

您需要编写一个递归函数来做到这一点,并让它自己调用.我尚未对此进行测试,但我认为它应该可以帮助您入门.我不会认可此函数的效率,因为它遍历数组中的每个项目并进行比较,即使您每次运行仅需要一个或两个项目(可能)也是如此.

You need to write a recursive function to do this and have it call itself. I haven't tested this out, but I think it should get you started. I wouldn't endorse this function's efficiency since it runs through every item in the array and does a comparison even though you're going to only need an item or two from each run (likely).

PHP:

$arr = array(...);
function output_lis($parentID = NULL){
    global $arr;
    $stack = array(); //create a stack for our <li>'s 
    foreach($arr as $a){ 
        $str = '';
            //if the item's parent matches the parentID we're outputting...
        if($a['parent']===$parentID){ 
            $str.='<li>'.$a['title'];

                    //Pass this item's ID to the function as a parent, 
                    //and it will output the children
            $subStr = output_lis($a['id']);
            if($subStr){
                $str.='<ul>'.$subStr.'</ul>';
            }

            $str.='</li>';
            $stack[] = $str;
        }
    }
    //If we have <li>'s return a string 
    if(count($stack)>0){
        return join("\n",$stack);
    }

    //If no <li>'s in the stack, return false 
    return false;
}

然后将其输出到您的页面上.像这样:

Then output this on your page. Something like:

<ul>
    <?php echo output_lis(); ?>
</ul>

这是我的示例数组:

$arr = array(
        array('title'=>'home','parent'=>NULL,'id'=>1), 
        array('title'=>'sub1','parent'=>1,'id'=>2), 
        array('title'=>'sub2','parent'=>1,'id'=>3), 
        array('title'=>'about us','parent'=>NULL,'id'=>4), 
        array('title'=>'sub3','parent'=>4,'id'=>5), 
        array('title'=>'sub4','parent'=>4,'id'=>6), 
    );

这篇关于PHP动态下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆