帮助PHP递归导航列表菜单

Jas*_*son 3 php navigation recursion html-lists

我正在尝试将动态递归导航列表菜单添加到我正在处理的网站.场景是菜单有两个与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>
Run Code Online (Sandbox Code Playgroud)

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

    <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>
Run Code Online (Sandbox Code Playgroud)

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

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我用以下方式调用菜单:

$menu = new Dynamic_Menu();
$menu->getMenu(1);
Run Code Online (Sandbox Code Playgroud)

有人可以帮助并解释我需要放置2级UL和/ UL标签的位置.在过去的两天里,我一直在用这个来敲打我的脑袋.任何帮助将不胜感激,谢谢......

Dan*_*iel 11

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

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 '';
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<ul>
    <li>Item 1</li>
    <li>Item 2
        <ul>
            <li>Item 2.1</li>
            <li>Item 2.2</li>
        </ul>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)