thara Posted October 17, 2015 Share Posted October 17, 2015 (edited) I am using this php function to display categories and sub-categories in a list. function displayMenuMobile(&$cats, $parent, $level=0) { switch ($level) { case 0: $class = "menu-phone"; $menuId = "nav"; break; case 1: $class = "sub-menu"; $menuId = ""; break; } if ($parent==0) { foreach ($cats[$parent] as $id=>$nm) { displayMenuMobile($cats, $id); } } else { echo "<ul class='$class' id='$menuId'>\n"; $classParent = ''; if ($level == 0) { $classParent = "class='parent'"; } foreach ($cats[$parent] as $id=>$nm) { echo "<li $classParent><a href='#'><span>$nm</span></a>\n"; if (isset($cats[$id])) { displayMenuMobile($cats, $id, $level+1); //increment level } } echo "</li></ul>\n"; } } Its displaying the list but it is not rendering HTML properly.This is rendering HTML: <ul class='menu-phone' id='nav'> <li class='parent'><a href='#'><span>Hot Deals</span></a> <li class='parent'><a href='#'><span>Pantry</span></a> <ul class='sub-menu' id=''> <li ><a href='#'><span>Biscuits</span></a> <li ><a href='#'><span>Canned Foods</span></a> <li ><a href='#'><span>Canned Vegetables</span></a> <li ><a href='#'><span>Chips, Snacks & Nuts</span></a> </ul> <li class='parent'><a href='#'><span>Drinks</span></a> <ul class='sub-menu' id=''> <li ><a href='#'><span>Coffee</span></a> <li ><a href='#'><span>Flavoured Milk Drinks</span></a> </ul> <li class='parent'><a href='#'><span>Confectionery</span></a> <ul class='sub-menu' id=''> <li ><a href='#'><span>Chocolate</span></a> <li ><a href='#'><span>Mints &Bathroom & Toilet Gums </span></a></li> </ul> <li class='parent'><a href='#'><span>Household</span></a> <ul class='sub-menu' id=''> <li ><a href='#'><span>Air Fresheners</span></a> <li ><a href='#'><span>Bathroom & Toilet</span></a> <li ><a href='#'><span>Cleaning</span></a> <li ><a href='#'><span>Electrical</span></a> <li ><a href='#'><span>Homeware</span></a> <li ><a href='#'><span>Laundry</span></a></li> </ul> <li class='parent'><a href='#'><span>Cosmetics</span></a> <ul class='sub-menu' id=''> <li ><a href='#'><span>Eye Makeup</span></a> <li ><a href='#'><span>Lip Gloss & Moisturisers</span></a> <li ><a href='#'><span>Nail Polish</span></a></li> </ul> </li> </ul> Problem is its not rendering closing </li> tag in the parent and nested list.Can anybody tell me whats wrong with this?Hope somebody may help me out.Thank you. Edited October 17, 2015 by thara Quote Link to comment https://forums.phpfreaks.com/topic/298658-php-fuction-not-rendering-html-properly/ Share on other sites More sharing options...
Ch0cu3r Posted October 17, 2015 Share Posted October 17, 2015 Because the closing <li> tag needs to be echo'd after the if echo "<li $classParent><a href='#'><span>$nm</span></a>\n"; if (isset($cats[$id])) { displayMenuMobile($cats, $id, $level+1); //increment level } echo "</li>\n"; // closing </li> tags needs to be echo'd here } echo "</ul>\n"; // not here I pretty sure Jacques1 explained that last time Quote Link to comment https://forums.phpfreaks.com/topic/298658-php-fuction-not-rendering-html-properly/#findComment-1523545 Share on other sites More sharing options...
thara Posted October 17, 2015 Author Share Posted October 17, 2015 Yes Sir, My mistake. I really miss understood it. Sorry for it. I have a another question, how I add "parent" class to "<li>" only if it has a sub-menu? Quote Link to comment https://forums.phpfreaks.com/topic/298658-php-fuction-not-rendering-html-properly/#findComment-1523549 Share on other sites More sharing options...
Ch0cu3r Posted October 17, 2015 Share Posted October 17, 2015 Change $classParent = ''; if ($level == 0) { $classParent = "class='parent'"; } foreach ($cats[$parent] as $id=>$nm) { to be foreach ($cats[$parent] as $id=>$nm) { $classParent = ($level == 0 && isset($cats[$id])) ? "class='parent'" : ''; Quote Link to comment https://forums.phpfreaks.com/topic/298658-php-fuction-not-rendering-html-properly/#findComment-1523559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.