HTML 列表结构中的 PHP 递归菜单

发布于 2024-10-20 07:47:50 字数 3178 浏览 1 评论 0原文

这是 html 结构,它应该是什么样子

<li><a href="#" class="menulink">Dropdown One</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li>
            <a href="#" class="sub">Navigation Item 2</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="sub">Navigation Item 3</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
                <li>
                    <a href="#" class="sub">Navigation Item 3</a>
                    <ul>
                        <li class="topline"><a href="#">Navigation Item 1</a></li>
                        <li><a href="#">Navigation Item 2</a></li>
                        <li><a href="#">Navigation Item 3</a></li>

                    </ul>
                </li>
                <li><a href="#">Navigation Item 4</a></li>
            </ul>
        </li>
        <li><a href="#">Navigation Item 4</a></li>
        <li><a href="#">Navigation Item 5</a></li>
    </ul>
</li>
<li><a href="#" class="menulink">Dropdown Two</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li><a href="#">Navigation Item 2</a></li>
    </ul>
</li>

,这是 mysql 表,

id |parent_id|        name     |
---+---------+-----------------+
 1 |    0    |Dropdown One     |
 2 |    1    |Navigation Item 1|
 3 |    1    |Navigation Item 2|
 4 |    3    |Navigation Item 1|
 5 |    3    |Navigation Item 2|
 6 |    1    |Navigation Item 3|
 7 |    6    |Navigation Item 1|
 8 |    6    |Navigation Item 2|
 9 |    6    |Navigation Item 3|
10 |    6    |Navigation Item 4|
11 |    1    |Navigation Item 4|
12 |    1    |Navigation Item 5|
13 |    0    |Dropdown Two     |
14 |   13    |Navigation Item 1|
15 |   13    |Navigation Item 2|

也许不是最好的方法,但这是我的 PHP 函数尝试:

function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    $i=1;
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0) {echo '<li><a href="#" class="menulink">'.$name.'</a></li>';}

        echo '<li><a href="#" class="menulink">'.$name.'</a></li>';
        //echo $name ." i: ".$i." level: ".$level."<br>";
        $i++;
        listCategory($id,$level+1);
    }
}

listCategory(0);

感谢您提前提供的任何帮助

here is the html structure how it should look like

<li><a href="#" class="menulink">Dropdown One</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li>
            <a href="#" class="sub">Navigation Item 2</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="sub">Navigation Item 3</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
                <li>
                    <a href="#" class="sub">Navigation Item 3</a>
                    <ul>
                        <li class="topline"><a href="#">Navigation Item 1</a></li>
                        <li><a href="#">Navigation Item 2</a></li>
                        <li><a href="#">Navigation Item 3</a></li>

                    </ul>
                </li>
                <li><a href="#">Navigation Item 4</a></li>
            </ul>
        </li>
        <li><a href="#">Navigation Item 4</a></li>
        <li><a href="#">Navigation Item 5</a></li>
    </ul>
</li>
<li><a href="#" class="menulink">Dropdown Two</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li><a href="#">Navigation Item 2</a></li>
    </ul>
</li>

and here is the mysql table

id |parent_id|        name     |
---+---------+-----------------+
 1 |    0    |Dropdown One     |
 2 |    1    |Navigation Item 1|
 3 |    1    |Navigation Item 2|
 4 |    3    |Navigation Item 1|
 5 |    3    |Navigation Item 2|
 6 |    1    |Navigation Item 3|
 7 |    6    |Navigation Item 1|
 8 |    6    |Navigation Item 2|
 9 |    6    |Navigation Item 3|
10 |    6    |Navigation Item 4|
11 |    1    |Navigation Item 4|
12 |    1    |Navigation Item 5|
13 |    0    |Dropdown Two     |
14 |   13    |Navigation Item 1|
15 |   13    |Navigation Item 2|

maybe not the best approach but here is my PHP function attempt:

function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    $i=1;
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0) {echo '<li><a href="#" class="menulink">'.$name.'</a></li>';}

        echo '<li><a href="#" class="menulink">'.$name.'</a></li>';
        //echo $name ." i: ".$i." level: ".$level."<br>";
        $i++;
        listCategory($id,$level+1);
    }
}

listCategory(0);

thanks for any help in advance

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

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

发布评论

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

评论(2

盛夏尉蓝 2024-10-27 07:47:50
function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    if(mysql_num_rows($res) == 0) return;
    echo '<ul>';
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0)
        {
           echo '<li><a href="#" class="menulink">'.$name.'</a>';
        }
        else
        {
           echo '<li><a href="#">'.$name.'</a>';
        }
        listCategory($id,$level+1);
        echo '</li>';
    }
    echo '</ul>';
}
function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    if(mysql_num_rows($res) == 0) return;
    echo '<ul>';
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0)
        {
           echo '<li><a href="#" class="menulink">'.$name.'</a>';
        }
        else
        {
           echo '<li><a href="#">'.$name.'</a>';
        }
        listCategory($id,$level+1);
        echo '</li>';
    }
    echo '</ul>';
}
泛滥成性 2024-10-27 07:47:50

您没有打开和关闭 UL 标签

You're not opening and closing your UL tags

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