0

I have an ordered tree (The nodes are in the correct order for output). Each node has a name, depth and parent_id properties. I am trying to output a nested list so I can visually see the structure. I am having a bit of trouble coming up with the right. What is the best way to fix this? Do I need to change my data structure and/or logic?

HERE IS DATA:

array(24) {
  [5]=>
  array(3) {
    ["depth"]=>
    int(0)
    ["name"]=>
    string(16) "Basketball Sport"
    ["parent_id"]=>
    NULL
  }
  [3]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(16) "Basketball Shoes"
    ["parent_id"]=>
    string(1) "5"
  }
  [2]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(6) "Jordon"
    ["parent_id"]=>
    string(1) "3"
  }
  [4]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(6) "Lebron"
    ["parent_id"]=>
    string(1) "3"
  }
  [7]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(17) "Basketball Shorts"
    ["parent_id"]=>
    string(1) "5"
  }
  [8]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(22) "Long Basketball Shorts"
    ["parent_id"]=>
    string(1) "7"
  }
  [9]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(23) "Short Basketball Shorts"
    ["parent_id"]=>
    string(1) "7"
  }
  [10]=>
  array(3) {
    ["depth"]=>
    int(0)
    ["name"]=>
    string(10) "Golf Sport"
    ["parent_id"]=>
    NULL
  }
  [12]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(4) "Bags"
    ["parent_id"]=>
    string(2) "10"
  }
  [13]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(9) "Nike Bags"
    ["parent_id"]=>
    string(2) "12"
  }
  [11]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(5) "Clubs"
    ["parent_id"]=>
    string(2) "10"
  }
  [14]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(10) "Nike Clubs"
    ["parent_id"]=>
    string(2) "11"
  }
  [6]=>
  array(3) {
    ["depth"]=>
    int(0)
    ["name"]=>
    string(12) "Tennis Sport"
    ["parent_id"]=>
    NULL
  }
  [1]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(8) "Racquets"
    ["parent_id"]=>
    string(1) "6"
  }
  [22]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(4) "Head"
    ["parent_id"]=>
    string(1) "1"
  }
  [24]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(6) "Prince"
    ["parent_id"]=>
    string(1) "1"
  }
  [23]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(6) "Wilson"
    ["parent_id"]=>
    string(1) "1"
  }
  [15]=>
  array(3) {
    ["depth"]=>
    int(1)
    ["name"]=>
    string(12) "Tennis Shoes"
    ["parent_id"]=>
    string(1) "6"
  }
  [16]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(17) "Mens Tennis Shoes"
    ["parent_id"]=>
    string(2) "15"
  }
  [19]=>
  array(3) {
    ["depth"]=>
    int(3)
    ["name"]=>
    string(23) "Cheap Mens Tennis Shoes"
    ["parent_id"]=>
    string(2) "16"
  }
  [18]=>
  array(3) {
    ["depth"]=>
    int(3)
    ["name"]=>
    string(27) "Expensive Mens Tennis Shoes"
    ["parent_id"]=>
    string(2) "16"
  }
  [17]=>
  array(3) {
    ["depth"]=>
    int(2)
    ["name"]=>
    string(19) "Womens Tennis Shoes"
    ["parent_id"]=>
    string(2) "15"
  }
  [20]=>
  array(3) {
    ["depth"]=>
    int(3)
    ["name"]=>
    string(28) "Women Practical Tennis Shoes"
    ["parent_id"]=>
    string(2) "17"
  }
  [21]=>
  array(3) {
    ["depth"]=>
    int(3)
    ["name"]=>
    string(26) "Women Stylish Tennis Shoes"
    ["parent_id"]=>
    string(2) "17"
  }
}

List output (NOTICE How it keeps getting deeper) :

List output

CODE:

<ul id="category_root">
        <?php 
        $current_depth = 0;

        foreach($categories as $category_id => $category_info)  
        {
            //Same level
            if ($current_depth == $category_info['depth'])
            {
                // echo 'SAME LEVEL'. $category_info['name'].'<br />';
                echo '<li class="same_level">'.$category_info['name'].'</li>';
            }//Sub category
            elseif ($current_depth < $category_info['depth'])
            {
                echo '<ul><li class="subcat_start">'.$category_info['name'];
            }//End sub category
            elseif($current_depth > $category_info['depth'])
            {
                echo '</li></ul>'.$category_info['name'];
            }


            $current_depth = $category_info['depth'];
        }
        ?>
</ul>
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • Why not rebuild your array structure like: `array(1) { [0] => array(2) { "name" => "basketball", "children" => array(2)` etc. – Daan Jun 29 '15 at 14:26
  • Instead of using a primitive when making a tree, use a class that represents it and have each node have children associated with it. you'll be way better in the long run, and can actually test it easily. – Amelia Jun 29 '15 at 14:27
  • I was thinking it was already ordered in the correct order and I should be able to use the change in depth to figure it out. How would I go about using children data structure? Is this possible with children of children? – Chris Muench Jun 29 '15 at 14:27
  • Yes it's possible with children of children – Daan Jun 29 '15 at 14:30
  • @Daan Could you show a minimal example of how I can use a children array? Ideally I would like to keep the current structure I already have; but if you don't see a way of using this then I don't mind changing. – Chris Muench Jun 29 '15 at 14:35
  • I found this http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre and am all set. Thank you for giving me the idea of creating a real tree – Chris Muench Jun 29 '15 at 14:54

0 Answers0