PHP 动态下拉菜单

发布于 2024-09-14 18:13:08 字数 737 浏览 3 评论 0原文

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

问题是,考虑到我需要正确的,我无法想到如何动态地执行此操作我的下拉菜单的

    • 结构。 这要求我在父页面的 foreach 中拥有子页面的“foreach”? 如果有道理的话,有解决办法吗?

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

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.

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)

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2024-09-21 18:13:09

您需要编写一个递归函数来执行此操作并让它调用自身。我还没有对此进行测试,但我认为它应该可以帮助您开始。我不会认可这个函数的效率,因为它会遍历数组中的每个项目并进行比较,即使您每次运行只需要一个或两个项目(可能)。

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;
}

然后将其输出到您的页面上。类似于:

<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), 
    );

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>

Here is my sample array:

$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), 
    );
清君侧 2024-09-21 18:13:09

无需递归。

$parents = array();
$noparents = array();
foreach ($results as $ar) {
  if ( $ar['parent'] != NULL ) {
    $parents[$ar['parent']] = array();
    array_push($parents[$ar['parent']], $ar['title']);
  } else {
    array_push($noparents, $ar['title']);
  }
}

现在,属于父级的所有链接都列在以父级命名的数组中。您的其他链接位于单独的数组中。如果你愿意的话,你可以加入他们。

根据您的应用程序的需要对其进行扩展。

No recursion necessary.

$parents = array();
$noparents = array();
foreach ($results as $ar) {
  if ( $ar['parent'] != NULL ) {
    $parents[$ar['parent']] = array();
    array_push($parents[$ar['parent']], $ar['title']);
  } else {
    array_push($noparents, $ar['title']);
  }
}

Now you have all of your links that belong to a parent listed in an array named after the parent. Your other links are in a separate array. You can join them if you like.

Extend it as is required for your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文