Created
May 28, 2010 02:05
-
-
Save navarr/416652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // $all_cats; | |
| // $categories; | |
| $completed_categories = array(); | |
| $level = 0; | |
| foreach($all_cats as $category) | |
| { | |
| if(!in_array($category["id"],$completed_categories)) // The category we are doing hasn't been done yet, Should indicate a parent. | |
| { | |
| print("<option value=\"{$category["id"]}\">{$category["name"]}</option>"); // Output | |
| if(count($category["children"])) // We have children | |
| { | |
| $level++; | |
| $level_ra = array(); | |
| $level_ra_int = array(); | |
| $level_ra[$level] = $category["children"]; | |
| $level_ra_int[$level] = 0; | |
| $child = $category["children"][0]; | |
| while(true) | |
| { | |
| if($child["id"]) | |
| { | |
| $prefix = str_repeat("--",$level); | |
| print("<option value=\"{$child["id"]}\">{$prefix} {$child["name"]}</option>"); | |
| if(count($category["children"])) | |
| { | |
| $level++; | |
| $level_ra[$level] = $child["children"]; | |
| $level_ra_int[$level] = 0; | |
| $child = $child["children"][0]; | |
| } | |
| else | |
| { | |
| $level_ra_int[$level]++; | |
| $child = $level_ra[$level][$level_ra_int[$level]]; | |
| } | |
| } | |
| else | |
| { | |
| if($level > 0) | |
| { | |
| $level--; | |
| $level_ra_int[$level]++; | |
| $child = $level_ra[$level][$level_ra_int[$level]]; | |
| } | |
| else | |
| { | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment