HTML 列表结构中的 PHP 递归菜单
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有打开和关闭 UL 标签
You're not opening and closing your UL tags