Jump to content

Function to create optgroups for related items


son
 Share

Recommended Posts

I have been working with this function to display data from db in select element. All working well, but now struggling with a nice touch to include the <optgroup /> element for items of same nav group. The code is:

function renderTree($parent = 0, $subCounter = 0, $navInit = 0){global $pid;global $categoryNR;global $categoryNav;    global $categoryNames;    global $childrenTree;	 if($parent != 0)  {  $catNavCounter = $categoryNav[$parent];   if ($catNavCounter != $navInit)   {   echo "<optgroup label=\"test\">";   $navInit = $navInit++;   }  echo "<option value=\"page.php?pid=" . $categoryNR[$parent] .  "\"";  if ((isset($pid)) && ($pid == $categoryNR[$parent]))   {   echo " selected=\"selected\" ";   }  echo " style=\"padding-left:" . $subCounter . "px;\">", $categoryNames[$parent] . " Navigation: " . $categoryNav[$parent] , "</option>\n";  $subCounter = $subCounter + 10;$catNavCounter = $categoryNav[$parent];   if ($catNavCounter != $navInit)   {   echo "</optgroup>";   }   }    $children = $childrenTree[$parent];	 if(count($children) > 0)  { //If node has children	    foreach($children as $child)	    renderTree($child, $subCounter);	 }  }renderTree();  //This renders the hierarchical tree}

Unfortunately my code creates an <optgroup /> element for each item... $categoryNav[$parent] shows fine in <option /> text, so issue is not related to data in array... Where am I going wrong? Son

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...