如何在 php 中迭代 ul 和 li 元素?
我正在 php 中创建动态菜单和子菜单...不知道从哪里开始...我已经编写了所有查询,但需要建议使用 Ul
和 li
元素......可能有可能没有子菜单的子菜单,可能有也可能没有菜单的子菜单......
Menu1
submenu1
submenu11
submenu12
submenu2
submenu21
Menu2
submenu1
submenu11
submenu12
任何建议......
编辑:
这是我正在做的事情,
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("testt");
echo '<ul id="nav">';
$res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
while($row=mysql_fetch_array($res))
{
echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
$res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
echo '<ul class="sub">';
while($row1=mysql_fetch_array($res1))
{
echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
$res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
echo '<ul>';
while($row2=mysql_fetch_array($res2))
{
echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
}
echo '</ul>';
}
echo '</ul>';
}
echo '</ul>';
?>
这是我的表格,
alt text http:// img229.imageshack.us/img229/1174/84296639.jpg我的 CSS 类似乎没有得到应用
I am creating dynamic menus and submenus in php.... Can't know where to start.... I have written all my queries but need suggestion to iterate the result set with Ul
and li
elements with it.... There may are may not be submenus for submenu and there may or may not be submenus for menus.....
Menu1
submenu1
submenu11
submenu12
submenu2
submenu21
Menu2
submenu1
submenu11
submenu12
Any suggestion....
EDIT:
Here is what i am doing,
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("testt");
echo '<ul id="nav">';
$res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
while($row=mysql_fetch_array($res))
{
echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
$res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
echo '<ul class="sub">';
while($row1=mysql_fetch_array($res1))
{
echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
$res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
echo '<ul>';
while($row2=mysql_fetch_array($res2))
{
echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
}
echo '</ul>';
}
echo '</ul>';
}
echo '</ul>';
?>
Here is my table,
alt text http://img229.imageshack.us/img229/1174/84296639.jpgMy CSS Class doesn't seem to get applied
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我的做法:
css文件:
产品或其他任何类别表的sql文件:
以及创建菜单的功能:
我希望这会对您有所帮助!
This is how i do:
The css file:
the sql file for products or anything else category table:
and the function that create menu:
I hope this will help you!
编辑:在您澄清问题之前发布。这是一般的递归菜单结构。
Edit: Posted before you clarified your question. Here's a general recursive menu structure.