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>开发者_如何学Go < 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.
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.
u must read this properly
jQuery UI TAB
精彩评论