Vel Posted April 25, 2012 Share Posted April 25, 2012 Hey guys, I have a bunch of categories and products in a tree format. For several reasons I need to restructure them from the tree format into a flattened format. I have 2 arrays, one full of categories, the other products. Each category and product has an ID, and a reference to it's parent's ID. The top level categories reference to 0. What I need is to have the categories output like thus: Top Level Categories: Category 1 Category 2 Category 1 Sub category 1 Sub category 2 Category 2 Sub category 3 Sub category 4 Sub category 1 Product 1 Product 2 Sub category 2 Product 3 Product 4 Sub category 3 Product 5 Product 6 Sub category 4 Product 7 Product 8 There could be an unlimited number of sub categories within categories before we get to products, so this needs to be done through a function, however I cannot for the life of me think how to do this. Initially I thought about using 2 arrays, a buffer of categories outputted, and a queue of categories to be outputted, but quickly realised that when I go more than 2 layers deep I can't keep track of the queue's properly. Quote Link to comment https://forums.phpfreaks.com/topic/261574-rearrange-tree-structure/ Share on other sites More sharing options...
Vel Posted April 25, 2012 Author Share Posted April 25, 2012 I managed to figure it out in the end. If anyone wants to see how here's the code: <?php /* Queue Manager */ function queue_manager($start_cat = 0) { $queue = array(); $num_queue = 0; $root = 0; for($i = 0; $i < count($this->cats); $i++) { if($this->cats[$i]['sectionID'] == $start_cat) { $root = $this->cats[$i]['rootSection']; } } if($root) { $this->output_product($start_cat); } else { $add_to_queue = $this->output_cat($start_cat); foreach($add_to_queue as $a) { $queue[$num_queue++] = $a; } } for($i = 0; $i < $num_queue; $i++) { $id = $this->cats[$queue[$i]]['sectionID']; $root = $this->cats[$queue[$i]]['rootSection']; if($root) { $this->output_product($id); } else { $add_to_queue = $this->output_cat($id); foreach($add_to_queue as $a) { $queue[$num_queue++] = $a; } } } } /* Output Category */ function output_cat($top) { echo '<div class="admin-tree-container">' . PHP_EOL; $output_buffer = array(); $return = array(); for($i = 0; $i < count($this->cats); $i++) { if($top == $this->cats[$i]['sectionID']) { echo '<div class="admin-tree-container-title">' . $this->cats[$i]['sectionWorkingName'] . '</div>' . PHP_EOL; } elseif($top == $this->cats[$i]['topSection']) { $return[] = $i; $li = '<li id="id' . $this->cats[$i]['sectionID'] . '"><span class="admin-tree-cat'; if($this->cats[$i]['sectionDisabled']) { $li .= ' cat-disabled'; } if($this->cats[$i]['rootSection']) { $li .= ' cat-root'; } $li .= '" id="id' . $this->cats[$i]['sectionID'] . '">' . $this->cats[$i]['sectionWorkingName'] . '</span></li>'; $output_buffer[] = $li; } } echo '<ul>' . PHP_EOL; foreach($output_buffer as $o) { echo $o . PHP_EOL; } echo '<li class="sort-button sort-cancel" id="btnid' . $cat . '">+ Add new product</li></ul></div>' . PHP_EOL; return $return; } /* Output Product */ function output_product($top) { echo '<div class="admin-tree-container">' . PHP_EOL; for($i = 0; $i < count($this->cats); $i++) { if($top == $this->cats[$i]['sectionID']) { echo '<div class="admin-tree-container-title">' . $this->cats[$i]['sectionWorkingName'] . '</div>' . PHP_EOL; $i = count($this->cats); } } echo '<ul>' . PHP_EOL; for($i = 0; $i < count($this->prods); $i++) { if($top == $this->prods[$i]['pSection']) { echo '<li class="sort-cancel" id="id' . $this->prods[$i]['pID'] . '"><span class="admin-tree-prod'; if(!$this->prods[$i]['pDisplay']) { echo ' prod-disabled'; } if(!$this->prods[$i]['pSell']) { echo ' prod-not-sell'; } echo '" id="id' . $this->prods[$i]['pID'] . '">' . $this->prods[$i]['pName'] . '</span></li>' . PHP_EOL; } } echo '<li class="sort-button sort-cancel" id="btnid' . $cat . '">+ Add new product</li></ul></div>' . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/261574-rearrange-tree-structure/#findComment-1340400 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.