Jump to content

need help! recursion issues


walshd

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/164666-need-help-recursion-issues/
Share on other sites

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(8) "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(8) "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(8) "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(8) "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(8) "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(8) "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(8) { [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" } } }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.