PHP 递归导航列表菜单帮助

发布于 2024-09-03 15:20:35 字数 4089 浏览 2 评论 0原文

我正在尝试向我正在处理的网站添加动态递归导航列表菜单。 场景是菜单有 2 个级别,通过 Parentid(preid) 相关。

我的问题是我可以正确显示第一级列表,但是我无法正确显示第二级列表。我不确定在哪里添加第二级的 UL 和 /UL 标签。

这就是我所追求的

<ul>
<li>Item 1</li>
<li>item 2</li>
<li>item 3</li>
<ul>
  <li>sub item 1</li>
  <li>sub item 2</li>
</ul>
<li>Item 4</li>
<li>item 5</li>
<ul>
  <li>sub item 1</li>
  <li>sub item 2</li>
</ul>
<li>item 6</li>
</ul>

这实际上是我通过以下代码得到的:

    <ul>
  <li>item 1
    <ul>
    </ul>
  </li>
  <li>item 2
    <ul>
      <li>sub item 1</li>
      <ul>
      </ul>
      <li>sub item 2</li>
      <ul>
      </ul>
    </ul>
  </li>
  <li>Sports Injuries
    <ul>
    </ul>
  </li>
    </ul>
  </li>
</ul>

下面是我用来创建菜单的类文件:

class Dynamic_Menu 
    {
        function getConfig()
        {
            $this->DB_SERVER = 'localhost';
            $this->DB_USER = '***';
            $this->DB_PASS = '***';
            $this->DB_NAME = '***';

        }

        function __construct()
        {
            $this->getConfig();
            $Conn = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS);
            if (!$Conn)
                die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn));
            $DB_select = mysql_select_db($this->DB_NAME, $Conn);
            if (!$DB_select)
                die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn));
        }

        function select_row($sql)
        {
            //echo $sql . "<br />";
            if ($sql!="")
            {
                $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
                if ($result)
                {
                    while($row = mysql_fetch_array($result))
                        $data[] = $row;
                }
                return $data;
            }
        }

        function recordCount($sql)
        {
            if ($sql!="")
            {
                $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
                if ($result)
                {
                    $cnt = mysql_num_rows($result);
                    return $cnt;
                }
            }
        }

        function getChild($id)
        {
            $menu = "";
            $str = "";
            $s = "SELECT * FROM vcms_sys_explorer WHERE preid = '$id' ";
            $res = $this->select_row($s);
            $menu .= '<ul>';
            for ($i=0;$i<count($res);$i++)
            {
                $cnt_of_child = $this->recordCount("SELECT * FROM vcms_sys_explorer where preid = '".$res[$i][eid]."' ");
                //if ($cnt_of_child > 0)
                //  $str = '';
                //else
                //  $str = " (is sub menu item)";

                $menu .= '<li>'. $res[$i][name].$str.'</li>';   
                $menu .= $this->getChild($res[$i][eid]);
            }
            $menu .= '</ul>';       
            return $menu;
        }

        function getMenu($parentid)
        {
            $menu = "";
            $s = "SELECT * FROM vcms_sys_explorer WHERE preid = '$parentid'  ";
            $res = $this->select_row($s);

            $menu .= '<ul>';

            for ($i=0;$i<count($res);$i++)
            { 
                $menu .= '<li>'.$res[$i][name].$this->getChild($res[$i][eid]).'</li>';
                if ((count($res) - 1) > $i) {
                }
            } 

            $menu .= '</ul>';

            return $menu;
        }
    }

我用以下内容调用菜单:

$menu = new Dynamic_Menu();
$menu->getMenu(1);

有人可以帮助并解释我需要在哪里放置关卡吗2 个 UL 和 /UL 标签。过去两天我一直在为此伤脑筋。 任何帮助将不胜感激,谢谢...

I am trying to add a dynamic recursive navigation list menu to a site of am working on.
The scenerio is that the menu has 2 levels related by a parentid(preid).

My issue is that I can display the 1st level list correctly, however I cannot get the second level to display properly. I am not sure where to add the UL and /UL tags for the second level.

This is what I am after

<ul>
<li>Item 1</li>
<li>item 2</li>
<li>item 3</li>
<ul>
  <li>sub item 1</li>
  <li>sub item 2</li>
</ul>
<li>Item 4</li>
<li>item 5</li>
<ul>
  <li>sub item 1</li>
  <li>sub item 2</li>
</ul>
<li>item 6</li>
</ul>

This is actually what i am getting with the below code:

    <ul>
  <li>item 1
    <ul>
    </ul>
  </li>
  <li>item 2
    <ul>
      <li>sub item 1</li>
      <ul>
      </ul>
      <li>sub item 2</li>
      <ul>
      </ul>
    </ul>
  </li>
  <li>Sports Injuries
    <ul>
    </ul>
  </li>
    </ul>
  </li>
</ul>

Below is the class file I am using to create the menu:

class Dynamic_Menu 
    {
        function getConfig()
        {
            $this->DB_SERVER = 'localhost';
            $this->DB_USER = '***';
            $this->DB_PASS = '***';
            $this->DB_NAME = '***';

        }

        function __construct()
        {
            $this->getConfig();
            $Conn = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS);
            if (!$Conn)
                die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn));
            $DB_select = mysql_select_db($this->DB_NAME, $Conn);
            if (!$DB_select)
                die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn));
        }

        function select_row($sql)
        {
            //echo $sql . "<br />";
            if ($sql!="")
            {
                $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
                if ($result)
                {
                    while($row = mysql_fetch_array($result))
                        $data[] = $row;
                }
                return $data;
            }
        }

        function recordCount($sql)
        {
            if ($sql!="")
            {
                $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
                if ($result)
                {
                    $cnt = mysql_num_rows($result);
                    return $cnt;
                }
            }
        }

        function getChild($id)
        {
            $menu = "";
            $str = "";
            $s = "SELECT * FROM vcms_sys_explorer WHERE preid = '$id' ";
            $res = $this->select_row($s);
            $menu .= '<ul>';
            for ($i=0;$i<count($res);$i++)
            {
                $cnt_of_child = $this->recordCount("SELECT * FROM vcms_sys_explorer where preid = '".$res[$i][eid]."' ");
                //if ($cnt_of_child > 0)
                //  $str = '';
                //else
                //  $str = " (is sub menu item)";

                $menu .= '<li>'. $res[$i][name].$str.'</li>';   
                $menu .= $this->getChild($res[$i][eid]);
            }
            $menu .= '</ul>';       
            return $menu;
        }

        function getMenu($parentid)
        {
            $menu = "";
            $s = "SELECT * FROM vcms_sys_explorer WHERE preid = '$parentid'  ";
            $res = $this->select_row($s);

            $menu .= '<ul>';

            for ($i=0;$i<count($res);$i++)
            { 
                $menu .= '<li>'.$res[$i][name].$this->getChild($res[$i][eid]).'</li>';
                if ((count($res) - 1) > $i) {
                }
            } 

            $menu .= '</ul>';

            return $menu;
        }
    }

I call the menu with:

$menu = new Dynamic_Menu();
$menu->getMenu(1);

Could someone please help and explain where I need to place the level 2 UL and /UL tags. I have been banging my head with this for the last 2 days.
Any help would be greatly appreciated, thanks...

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

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

发布评论

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

评论(2

离去的眼神 2024-09-10 15:20:35

在嵌套列表中,子列表将始终包含在列表元素内 - 这就是它们嵌套的原因。您可以使用这种格式仅在一个函数中打印完整列表(在通用代码中,但您应该了解基本概念):

function get_list($parent) {
    $children = query('SELECT * FROM table WHERE parent_id = '.$parent);
    $items = array();
    while($row = fetch_assoc($children)) {
        $items[] = '<li>'.$row['name'].get_list($row['id']).'</li>';
    }
    if(count($items)) {
        return '<ul>'.implode('', $items).'</ul>';
    } else {
        return '';
    }
}

这将为您提供一个结构正确的列表,如下所示:

<ul>
    <li>Item 1</li>
    <li>Item 2
        <ul>
            <li>Item 2.1</li>
            <li>Item 2.2</li>
        </ul>
    </li>
</ul>

In a nested list, sub-lists will always be contained within a list element-- that's what makes them nested. You can print a full list in just one function using this format (in generic code, but you should get the basic idea):

function get_list($parent) {
    $children = query('SELECT * FROM table WHERE parent_id = '.$parent);
    $items = array();
    while($row = fetch_assoc($children)) {
        $items[] = '<li>'.$row['name'].get_list($row['id']).'</li>';
    }
    if(count($items)) {
        return '<ul>'.implode('', $items).'</ul>';
    } else {
        return '';
    }
}

And this will give you a list structured properly as:

<ul>
    <li>Item 1</li>
    <li>Item 2
        <ul>
            <li>Item 2.1</li>
            <li>Item 2.2</li>
        </ul>
    </li>
</ul>
灼疼热情 2024-09-10 15:20:35

尽管这个问题与问题并不完全相同我两天前发布了,这是我尝试使用文件夹而不是数据库进行操作的结果。下面将遍历指定 $path 的目录和所有子目录,并在脚本运行完成后将结果输出到嵌套的无序列表中。希望有帮助。

<?php
function readDirs($path){
    $dirHandle = opendir($path);
    echo "<ul>";
    while ($item = readdir($dirHandle)) {
        $newPath = $path . "/" . $item;

        if (is_dir($newPath) && $item != '.' && $item != '..') {
            echo "<li><a href='$newPath'>$item</a>";
            readDirs($newPath);
        }
    }
    echo "</li></ul>";
}
$path = "./galleries";
readDirs($path);

?>

All though this question is not the exact same as the question I posted 2 days ago, here is the result of what I was attempting to do with folders rather than a DB. The following will traverse the directory and all sub directories of the specified $path and spits out the results in a nested un-ordered list upon completion of running the script. Hope it helps.

<?php
function readDirs($path){
    $dirHandle = opendir($path);
    echo "<ul>";
    while ($item = readdir($dirHandle)) {
        $newPath = $path . "/" . $item;

        if (is_dir($newPath) && $item != '.' && $item != '..') {
            echo "<li><a href='$newPath'>$item</a>";
            readDirs($newPath);
        }
    }
    echo "</li></ul>";
}
$path = "./galleries";
readDirs($path);

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