Flatten Tree Structure Output html List (php) -


i have ordered tree (the nodes in correct order output). each node has name, depth , parent_id properties. trying output nested list can visually see structure. having bit of trouble coming right. best way fix this? need change data structure and/or logic?

here 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 keeps getting deeper) :

list output

code:

<ul id="category_root">         <?php          $current_depth = 0;          foreach($categories $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> 


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -