简体   繁体   English

PHP / MySQL导航菜单

[英]PHP/MySQL Navigation Menu

I'm trying to create a function for a hierarchical navigational menu bar. 我正在尝试为分层的导航菜单栏创建一个函数。

I want to be able to have something like this... 我希望能够有这样的东西...

<ul id="navigation">
<li><a href="#">Menu Item 1</a></li>
<li><a href="#">Menu Item 2</a></li>
    <ul>
        <li><a href="#">Sub Menu Item 1</a></li>
        <li><a href="#">Sub Menu Item 1</a></li>
    </ul>
<li><a href="#">Menu Item 3</a></li>
<li><a href="#">Menu Item 4</a></li>
</ul>

I'm using this function, but it's not working properly like I'd like it to. 我正在使用此功能,但无法正常运行。 It's showing the main parent links, but not the child links. 它显示的是主要的父链接,而不是子链接。

function build_navbar($pid,$sub=0)
{
    global $db;

    $query = $db->simple_select("navbar", "*", "pid='".$pid."'", array("order_by" => "disporder"));
    $menu_build = "<ul id=\"navigation\">\n";
    while($menu = $db->fetch_array($query))
    {   
        if($sub == 1)
        {
           $menu_build .= "<ul>\n";
           $menu_build .= "<li><a href=\"#\">".$menu['title']."</a></li>\n";
           $menu_build .= "</ul>\n";
        }
        else
        {
           $menu_build .= "<li><a href=\"#\">".$menu['title']."</a></li>\n";
        }
        build_navbar($menu['id'],1);
    }
    $menu_build .= "</ul>\n";

    return $menu_build;
}

Perhaps someone can help me fix this? 也许有人可以帮我解决这个问题? Thanks. 谢谢。

--- New Update --- -新更新-

Andy Groff, this is what your code is outputting: Andy Groff,这是您的代码输出的内容:

<ul id="navigation">
    <li><a href="#">Home</a></li>
    <ul>
        <li><a href="#">Child Link</a></li>
        <li><a href="#">Child 2</a></li>
    </ul>
    <li><a href="#">Parent</a></li>
</ul>

However, I need it modified so it'll output like this: 但是,我需要对其进行修改,以便其输出如下:

<ul id="navigation">
    <li><a href="#">Home</a>
        <ul>
            <li><a href="#">Child Link</a></li>
            <li><a href="#">Child 2</a></li>
        </ul>
    </li>
    <li><a href="#">Parent</a></li>
</ul>

This is what it is outputting now, Andy: 这是现在输出的内容,Andy:

<ul id="navigation">
    </li>
    <li><a href="#">Home</a>
    </li>
    <ul>
        <li>
        <a href="#">Child Link</a>
        </li>
        <li><a href="#">Child 2</a>
    </ul>
    </li>
    <li><a href="#">Parent</a>
</ul>

I think your problem could have something to do with the fact that your function is recursive, but the string you're building gets reset at the top of your function each time, instead of being passed into the function again. 我认为您的问题可能与函数是递归的这一事实有关,但是您正在构建的字符串每次都在函数顶部重置,而不是再次传递给函数。 Also, I don't see anywhere that sub will get set back to zero for your final iteration. 另外,在最终迭代中,我看不到任何地方该子项都将设置回零。 Additionally, it seems like you shouldn't need to query for each individual row. 此外,似乎您不需要查询每一行。 It would be more effective to query once and build the whole menu. 查询一次并构建整个菜单会更有效。 I think the recursion can be ditched. 我认为可以放弃递归。 Also, I would recommend storing a "sub" flag in your data, instead of using some hard to understand php logic as to whether or not a given row is a sub menu. 另外,我建议在您的数据中存储一个“ sub”标志,而不是使用一些难以理解的php逻辑来确定给定的行是否是子菜单。 I made modifications based of these concepts, no idea if it works or not though since I don't have/want to create the data to test it: 我根据这些概念进行了修改,但不知道它是否有效,因为我没有/想要创建数据来对其进行测试:

function build_navbar()
{
    global $db;
    //first things first, i'd recommend putting a "sub" flag in your database. This example will use it.

    //start off by getting all of the rows. No need for recursion.
    $query = $db->simple_select("navbar", "*", "1", array("order_by" => "disporder"));
    $menu_build = "<ul id=\"navigation\">\n";

    //keep track of what level we're at
    $level = 1;

    while($menu = $db->fetch_array($query))
    {
      //get sub from data
      $sub = $menu['sub']

      //we need to go back to root level
      if($sub == 0 && $level == 2){
        $level--;
        $menu_build .= "</ul></li>\n";
      }
      else $menu_build .= "</li>\n";

      //we need to go up one level
      if($sub == 1 && $level == 1)
      {
        $level++;
        $menu_build .= "<ul><li>\n";
      }
      else $menu_build .= "<li>";
      //always print out a link
      $menu_build .= "<a href=\"#\">".$menu['title']."</a>\n";
    }
    $menu_build .= "</ul>\n";

    return $menu_build;
}

UPDATE: 更新:

Try this: 尝试这个:

function build_navbar($pid, $sub=0)
{
    global $db;

    $class = $sub ? "sub" : "navigation";

    $menu_build = "<ul class=\"$class\">\n";

    $query = $db->simple_select("navbar", "*", "pid='".$pid."'");
    while($menu = $db->fetch_array($query))
    {
        $menu_build .= "<li><a href=\"#\">".$menu['title']."</a>\n";

        // build child links
        $menu_build .= build_navbar($menu['id'],1);
    }

    $menu_build .= "</ul>";

    return $menu_build;
}

What we are doing here, is allowing each function to build a <ul><li> group, the $sub variable will determine what the ID of the <ul> will be, allowing you to theme each ul differently. 我们在这里所做的是允许每个函数建立一个<ul><li>组, $sub变量将确定<ul>的ID,从而使您可以以不同的方式设置每个ul的主题。


EDIT: 编辑:

I found it! 我找到了!

this line 这条线

build_navbar($menu['id'],1);

needs to be changed to this: 需要更改为:

$menu_build = build_navbar($menu['id'],1);

It looks like it should work to me. 看来它应该对我有用。

What I would do is add some echo statements displaying the SQL query that is being executed each time, then you can copy that into phpmyadmin (or what ever db browser you use). 我要做的是添加一些显示每次执行的SQL查询的echo语句,然后可以将其复制到phpmyadmin(或使用的任何数据库浏览器)中。 See if it also returns a null result. 查看它是否还返回空结果。 If it does, there might be something wrong with your data as well. 如果是这样,您的数据也可能有问题。

so for example: 因此,例如:

echo "SELECT FROM navbar * WHERE pid='$pid' ORDER_BY disporder;<br>";

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

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