php - Nested option in select -
i'm making taxonomy system. created recursive function , function display categories in list (<ul>
). works fine list, every child element move right...etc...
but need same <select>
. here i've done far :
private function _tohtml(&$list, $parent=-1, $pass = 0){ $foundsome = false; for( $i=0,$c=count($list);$i<$c;$i++ ){ $delay = ''; if( $list[$i]['parent_id']==$parent ){ if( $foundsome==false ){ $delay .= str_repeat("--", $pass); $foundsome = true; } echo '<option value="'. $list[$i]['id'] .'">'. $delay . $list[$i]['name'] .'</option>'; $this->_tohtml($list,$list[$i]['id'],$pass+1); } } }
the problem is, works on first child elment, others kind of not considering @ all. looks :
cinema --english (sub cat cinema) french (also sub cat cinema)
when expect have :
cinema --english (sub cat cinema) --french (also sub cat cinema)
any idea?
thanks help
whenever face such problems, more straight forward , forget premature optimization. simplify function reducing nesting , proper naming of variables.
private function _tohtml($list, $parent = -1, $level = 0) { foreach ($list $entry) { if ($entry['parent_id'] != $parent) { continue; } printf('<option value="%s">%s%s</option>', $entry['id'], str_repeat('--', $level), $entry['name'] ); $this->_tohtml($list, $entry['id'], $level + 1); } }
Comments
Post a Comment