Hi, This is my php code for category UL and LI recursive. Now is working fine but the output is not what I expected.
Here is the HTML output of the code I provided:
<ul><li> <a href="#">Electronic Gadgets </a><span class="subDropdown plus"></span><ul><li> <a href="#">Pant</a></li><li> <a href="#">Aircond</a></li><li> <a href="#">Shoe</a></li><li> <a href="#">test</a></li><li> <a href="#">hehe</a></li><li> <a href="#">hahahhehe</a></li><li> <a href="#">hehe</a></li><li> <a href="#">hahahhehe</a></li><li> <a href="#">te</a></li><li> <a href="#">te</a></li><li> <a href="#">te</a></li><li> <a href="#">te</a></li></ul></li><li> <a href="#">Fashion & Accessory </a></li><li> <a href="#">Beauty & Wellness </a><ul><li> <a href="#">Test</a></li><li> <a href="#">test</a></li><li> <a href="#">Test</a></li><li> <a href="#">test</a></li></ul></li><li> <a href="#">Food & Beverages </a></li><li> <a href="#">Baby, Kids & Mommy </a></li><li> <a href="#">Sports & Outdoor </a></li><li> <a href="#">Home</a></li><li> <a href="#">Cars & Bikes </a></li><li> <a href="#">Pets World</a></li></ul>
Here is my PHP:
function showMenu($items, $parent = null,$level = 0) {
// Assuming root elements have parent_id of 0:
$index = $parent == null ? '0' : $parent;
if (empty($items[$index])) {
// No children, don't output anything
return;
}
echo '<ul', $parent == null ? '' : '', '>';
foreach ($items[$index] as $child) {
echo '<li> <a href="#">', htmlentities($child['Name']).'</a>';
if($level == 0){
echo'<span class="subDropdown plus"></span>';
}
showMenu($items, $child['ID'],$level= 1);
echo '</li>';
}
echo '</ul>';
}
$items = array();
$res = mysql_query("SELECT * FROM category");
while ($row = mysql_fetch_assoc($res)) {
$items[$row['Parent_ID']][] = $row;
}
echo showMenu($items);
How can I make it like if there is a children will add <span class="subDropdown plus">
after LI tag and before UL tag. My output only display one time <span class="subDropdown plus">
. The other category that have children are not display <span class="subDropdown plus">
. Please help me