make a tree of items in selectbox in php -


i have code make tree in html select box i've filled array , generate tree in select box :

$vertices = array(     array(         'id' => 5,         'name' => 'child3',         'parent' => 2,     ),     array(         'id' => 3,         'name' => 'child1',         'parent' => 1,     ),     array(         'id' => 4,         'name' => 'child2',         'parent' => 1,     ),     array(         'id' => 1,         'name' => 'cat1',         'parent' => null,     ),     array(         'id' => 2,         'name' => 'cat2',         'parent' => null,     ), );  /**  * generate tree  */ $subtrees = $trees = array(); foreach ($vertices $vertex) {     $v = array(         'id' => $vertex['id'],         'name' => $vertex['name'],         'children' => array(),     );      if (isset($subtrees[$vertex['id']])) {         $v['children'] = $subtrees[$vertex['id']];     }      if ($vertex['parent'] === null) {         $trees[] = $v;     }     else if (!isset($subtrees[$vertex['parent']])) {         $subtrees[$vertex['parent']] = array($v);     }     else {         $subtrees[$vertex['parent']][] = $v;     } } unset($subtrees);  /**  * generate html  */  function getsubtreeoptions(array $subtreeroot, $level = 0) {     $html = sprintf('%s<option value="%d">%s%s</option>' . php_eol,         str_repeat("\t", $level + 1),         $subtreeroot['id'],         $level > 0 ? str_repeat(' ', $level) . '--' : null,         $subtreeroot['name']);      foreach ($subtreeroot['children'] $child) {         $html .= getsubtreeoptions($child, $level + 1);     }      return $html; }  echo '<select name="stuff">' . php_eol; foreach ($trees $root) {     echo getsubtreeoptions($root); } echo '</select>'; 

it works fine , when want fill array mysql query, shows nothing in selectbox

this db code fill array :

$res = mysql_query('select id, name, parent groups order parent desc, id') or trigger_error('query failed: ' . mysql_error(), e_user_error); $vertices = array(); while ($vertex = mysql_fetch_assoc($res)) {     $vertices[] = $vertex; } 

where problem?

when print array.the output :

array (     [0] => array         (             [id] => 4             [name] => test             [parent] => 3         )      [1] => array         (             [id] => 2             [name] => test             [parent] => 1         )      [2] => array         (             [id] => 3             [name] => test             [parent] => 1         )      [3] => array         (             [id] => 1             [name] => test             [parent] => 0         )      [4] => array         (             [id] => 5             [name] => test             [parent] => 0         )  ) 

replace 0 null parent key in $verties nodes have no children , should fine. $vertices array should like

array ( [0] => array     (         [id] => 4         [name] => test         [parent] => 3     )  [1] => array     (         [id] => 2         [name] => test         [parent] => 1     )  [2] => array     (         [id] => 3         [name] => test         [parent] => 1     )  [3] => array     (         [id] => 1         [name] => test         [parent] => null     )  [4] => array     (         [id] => 5         [name] => test         [parent] => null     )   ) 

the reason being in condition

 if ($vertex['parent'] === null) {     $trees[] = $v; } 

you against null


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -