walshd Posted July 3, 2009 Share Posted July 3, 2009 HI I have the following code to look at the db and produce a nested set of unordered lists. the script appears to work fine until I try and validate my code then it is apparent that there is an issue with the recursion and i just do not know how to get around it. this is where you experts may be able to help me. the following code basically prints an opening ul tag for every line and sometimes does not close the ul off before starting the next. what I am after is basically a set of ul lists that look like: <ul> <li>parent 1 name</li> <li>child name</li> <li>child 2 name</li> </ul> //end parent 1 <ul> <li>parent 2 name</li> <li> child 1 parent 2 </li> </ul> // end of parent 2 <ul> <li>parent3</li> </ul> // end of parent 3 <ul> <li>parent 4</li> <li>child 1 parent 4 </li> <li>child 2 parent 4</li> </ul> //end of parent 4 the script I have is: <?php if(@$_SESSION["clientLoginLevel"] != "") $minloglevel=$_SESSION["clientLoginLevel"]; else $minloglevel=0; $result = mysql_query( "SELECT sectionID, sectionName, topSection, rootSection, sectionOrder, sectionDisabled FROM sections WHERE sectionDisabled<=" . $minloglevel . " ORDER BY sectionName" ); // prepare special array with parent-child relations $menuData = array( 'items' => array(), 'parents' => array() ); while ($menuItem = mysql_fetch_assoc($result)) { $menuData['items'][$menuItem['sectionID']] = $menuItem; $menuData['parents'][$menuItem['topSection']][] = $menuItem['sectionID']; } function buildMenu($parentId, $menuData, $dept) { if (isset($menuData['parents'][$parentId])) { echo '<ul class="catNav">'; foreach ($menuData['parents'][$parentId] as $itemId) { echo '<li><a href="products.php?cat=' . $menuData['items'][$itemId]['sectionName'] . '">' . $menuData['items'][$itemId]['sectionName'].'</a></li>'; if($dept>1) { echo buildMenu($itemId, $menuData, $dept-1); } } echo '</ul>'; } } // output the menu echo buildMenu(0, $menuData, 5); ?> Any help or advice would be great. I have tried everyhitng I can think of so a few sets of fresh eyes and probablly much better php knowledge than mine may just do the trick. Dave Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/ Share on other sites More sharing options...
backie Posted July 3, 2009 Share Posted July 3, 2009 Well seeing a var dump of the $menuData would be helpful. But from what I can see is your reusing the exact same array in a loop within foreach of the array and there is no place to stop if $dept has been reached. Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/#findComment-868361 Share on other sites More sharing options...
walshd Posted July 3, 2009 Author Share Posted July 3, 2009 doesn't it stop the loop if $dept is 1 or under and then simply echo the close ul? Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/#findComment-868362 Share on other sites More sharing options...
walshd Posted July 3, 2009 Author Share Posted July 3, 2009 here is the var dump asked for: array(2) { ["items"]=> array(24) { [9]=> array(6) { ["sectionID"]=> string(1) "9" ["sectionName"]=> string(3) "aaa" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "9" ["sectionDisabled"]=> string(1) "0" } [21]=> array(6) { ["sectionID"]=> string(2) "21" ["sectionName"]=> string(11) "Accessories" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(2) "21" ["sectionDisabled"]=> string(1) "0" } [22]=> array(6) { ["sectionID"]=> string(2) "22" ["sectionName"]=> string(4) "Bags" ["topSection"]=> string(2) "21" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "22" ["sectionDisabled"]=> string(1) "0" } [5]=> array(6) { ["sectionID"]=> string(1) "5" ["sectionName"]=> string(14) "Computer Parts" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(1) "1" ["sectionDisabled"]=> string(1) "0" } [10]=> array(6) { ["sectionID"]=> string(2) "10" ["sectionName"]=> string( "Football" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(2) "10" ["sectionDisabled"]=> string(1) "0" } [12]=> array(6) { ["sectionID"]=> string(2) "12" ["sectionName"]=> string(14) "Football Boots" ["topSection"]=> string(2) "10" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "12" ["sectionDisabled"]=> string(1) "0" } [11]=> array(6) { ["sectionID"]=> string(2) "11" ["sectionName"]=> string(15) "Football Shirts" ["topSection"]=> string(2) "10" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "11" ["sectionDisabled"]=> string(1) "0" } [16]=> array(6) { ["sectionID"]=> string(2) "16" ["sectionName"]=> string(18) "Football team gear" ["topSection"]=> string(2) "10" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(2) "16" ["sectionDisabled"]=> string(1) "0" } [19]=> array(6) { ["sectionID"]=> string(2) "19" ["sectionName"]=> string( "Footwear" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(2) "19" ["sectionDisabled"]=> string(1) "0" } [23]=> array(6) { ["sectionID"]=> string(2) "23" ["sectionName"]=> string(4) "Hats" ["topSection"]=> string(2) "21" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "23" ["sectionDisabled"]=> string(1) "0" } [7]=> array(6) { ["sectionID"]=> string(1) "7" ["sectionName"]=> string(3) "men" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "7" ["sectionDisabled"]=> string(1) "0" } [3]=> array(6) { ["sectionID"]=> string(1) "3" ["sectionName"]=> string(11) "Peripherals" ["topSection"]=> string(1) "5" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "2" ["sectionDisabled"]=> string(1) "0" } [4]=> array(6) { ["sectionID"]=> string(1) "4" ["sectionName"]=> string( "Printers" ["topSection"]=> string(1) "6" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "6" ["sectionDisabled"]=> string(1) "0" } [6]=> array(6) { ["sectionID"]=> string(1) "6" ["sectionName"]=> string(21) "Printers and Scanners" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(1) "4" ["sectionDisabled"]=> string(1) "0" } [13]=> array(6) { ["sectionID"]=> string(2) "13" ["sectionName"]=> string(5) "Rugby" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "0" ["sectionOrder"]=> string(2) "13" ["sectionDisabled"]=> string(1) "0" } [15]=> array(6) { ["sectionID"]=> string(2) "15" ["sectionName"]=> string(11) "Rugby Boots" ["topSection"]=> string(2) "13" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "15" ["sectionDisabled"]=> string(1) "0" } [14]=> array(6) { ["sectionID"]=> string(2) "14" ["sectionName"]=> string(17) "Rugby Shirts/kits" ["topSection"]=> string(2) "13" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "14" ["sectionDisabled"]=> string(1) "0" } [2]=> array(6) { ["sectionID"]=> string(1) "2" ["sectionName"]=> string( "Scanners" ["topSection"]=> string(1) "6" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "5" ["sectionDisabled"]=> string(1) "0" } [8]=> array(6) { ["sectionID"]=> string(1) "8" ["sectionName"]=> string( "Specials" ["topSection"]=> string(1) "0" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "8" ["sectionDisabled"]=> string(1) "0" } [24]=> array(6) { ["sectionID"]=> string(2) "24" ["sectionName"]=> string(10) "Sunglasses" ["topSection"]=> string(2) "21" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "24" ["sectionDisabled"]=> string(1) "0" } [1]=> array(6) { ["sectionID"]=> string(1) "1" ["sectionName"]=> string(7) "Systems" ["topSection"]=> string(1) "5" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(1) "3" ["sectionDisabled"]=> string(1) "0" } [18]=> array(6) { ["sectionID"]=> string(2) "18" ["sectionName"]=> string(11) "third level" ["topSection"]=> string(2) "16" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "18" ["sectionDisabled"]=> string(1) "0" } [20]=> array(6) { ["sectionID"]=> string(2) "20" ["sectionName"]=> string( "trainers" ["topSection"]=> string(2) "19" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "20" ["sectionDisabled"]=> string(1) "0" } [17]=> array(6) { ["sectionID"]=> string(2) "17" ["sectionName"]=> string(18) "training equipment" ["topSection"]=> string(2) "16" ["rootSection"]=> string(1) "1" ["sectionOrder"]=> string(2) "17" ["sectionDisabled"]=> string(1) "0" } } ["parents"]=> array( { [0]=> array(9) { [0]=> string(1) "9" [1]=> string(2) "21" [2]=> string(1) "5" [3]=> string(2) "10" [4]=> string(2) "19" [5]=> string(1) "7" [6]=> string(1) "6" [7]=> string(2) "13" [8]=> string(1) "8" } [21]=> array(3) { [0]=> string(2) "22" [1]=> string(2) "23" [2]=> string(2) "24" } [10]=> array(3) { [0]=> string(2) "12" [1]=> string(2) "11" [2]=> string(2) "16" } [5]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "1" } [6]=> array(2) { [0]=> string(1) "4" [1]=> string(1) "2" } [13]=> array(2) { [0]=> string(2) "15" [1]=> string(2) "14" } [16]=> array(2) { [0]=> string(2) "18" [1]=> string(2) "17" } [19]=> array(1) { [0]=> string(2) "20" } } } Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/#findComment-868363 Share on other sites More sharing options...
sasa Posted July 3, 2009 Share Posted July 3, 2009 remove echo command before calling your function buildMenu Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/#findComment-868414 Share on other sites More sharing options...
walshd Posted July 3, 2009 Author Share Posted July 3, 2009 Thanks sasa. tried that but still it will not validate. any more ideas? From research I think it is because the child ul's are directly under the parent ul and these need to be in a li of the parent but for the life of me I can not get this work error free. Quote Link to comment https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/#findComment-868519 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.