根据类别生成无序列表
我需要从类别表中填充无序列表。
类别: 0->代表父类别。
id 姓名 父母 1 n1 0 2 n2 1 3 n3 2 4 n4 3 5 n5 1 6 n6 0 ........
我尝试使用以下内容:
$menu_array 是包含类别表作为关联数组的数组。
while ( $row = mysql_fetch_assoc($query) ) {
$menu_array[$row['id']] = array('name' => $row['name'],'parent' => $row['parent']);
}
function generate_menu($parent)
{
//this prevents printing 'ul' if we don't have subcategories for this category
$has_childs = false;
//use global array variable instead of a local variable to lower stack memory requierment
global $menu_array;
foreach($menu_array as $key => $value)
{
if ($value['parent'] == $parent)
{
//if this is the first child print '<ul>'
if ($has_childs === false)
{
//don't print '<ul>' multiple times
$has_childs = true;
echo '<ul>';
}
echo '< li>< a href="/category/' . $value['name'] . '/">' . $value['name'] . '< /a>';
generate_menu($key);
//call function again to generate nested list for subcategories belonging to this category
echo '< /li>';
}
}
if ($has_childs === true) echo '< /ul>';
}
当在服务器中运行上述代码时,我得到 html 输出(查看源代码):
< ul>
<李>
< a href="#">站点模板< /a>
< ul>
<力>< a href="#">广告素材< /a>
< ul>
<力>< a href="#">作品集< /a>< /li>
<力>< a href="#">摄影< /a>< /li>
<力>< a href="#">艺术< /li>
< /ul>
< /li>
......
I want to get like:
< ul id="jsddm">
< li>< a href="#">Parent Category 1< /a>
< ul>
< li><a href="#">Sub Category 1 - 1< /a>< /li>
< li><a href="#">Sub Category 2 - 1< /a>< /li>
< li><a href="#">Sub Category 3 - 1< /a>< /li>
< /ul>
</ li>
< li><a href="#">Parent Category 2< /a>< /li>
< /ul>
任何帮助将不胜感激。
I need un-ordered list to be populated from categories table.
categories:
0 -> represents parent category.
id name parent
1 n1 0
2 n2 1
3 n3 2
4 n4 3
5 n5 1
6 n6 0
........
I tried using following:
$menu_array is array that contains the categories table as associative array.
while ( $row = mysql_fetch_assoc($query) ) {
$menu_array[$row['id']] = array('name' => $row['name'],'parent' => $row['parent']);
}
function generate_menu($parent)
{
//this prevents printing 'ul' if we don't have subcategories for this category
$has_childs = false;
//use global array variable instead of a local variable to lower stack memory requierment
global $menu_array;
foreach($menu_array as $key => $value)
{
if ($value['parent'] == $parent)
{
//if this is the first child print '<ul>'
if ($has_childs === false)
{
//don't print '<ul>' multiple times
$has_childs = true;
echo '<ul>';
}
echo '< li>< a href="/category/' . $value['name'] . '/">' . $value['name'] . '< /a>';
generate_menu($key);
//call function again to generate nested list for subcategories belonging to this category
echo '< /li>';
}
}
if ($has_childs === true) echo '< /ul>';
}
When run the above code in server, I get html output(view source):
< ul>
< li>
< a href="#">Site Templates< /a>
< ul>
< li>< a href="#">Creative< /a>
< ul>
< li>< a href="#">Portfolio< /a>< /li>
< li>< a href="#">Photography< /a>< /li>
< li>< a href="#">Art< /li>
< /ul>
< /li>
.....
I want to get like:
< ul id="jsddm">
< li>< a href="#">Parent Category 1< /a>
< ul>
< li><a href="#">Sub Category 1 - 1< /a>< /li>
< li><a href="#">Sub Category 2 - 1< /a>< /li>
< li><a href="#">Sub Category 3 - 1< /a>< /li>
< /ul>
</ li>
< li><a href="#">Parent Category 2< /a>< /li>
< /ul>
any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了我自己问题的答案。我试图实现多级下拉列表,但使用的是单个下拉列表。感谢您的评论。
I got the answer for my own question. I was trying to implement multi-level drop down but was using single drop down list. Thanks for the comments.
你必须正确阅读本文
jQuery UI 选项卡
u must read this properly
jQuery UI TAB