son Posted April 15, 2012 Share Posted April 15, 2012 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 More sharing options...
justsomeguy Posted April 16, 2012 Share Posted April 16, 2012 If the condition in your if statement is always true then you'll need to figure out why that's the case, or if you should be using a different condition to check for that. Link to comment Share on other sites More sharing options...
son Posted April 18, 2012 Author Share Posted April 18, 2012 Will check it carefully again. ThanksSon Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.