Skip to content

Instantly share code, notes, and snippets.

@navarr
Created May 28, 2010 02:05
Show Gist options
  • Select an option

  • Save navarr/416652 to your computer and use it in GitHub Desktop.

Select an option

Save navarr/416652 to your computer and use it in GitHub Desktop.
<?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