php - How to properly render the same thing as drop down-list, but in selectbox? -


there's array looks so:

array (     [items] => array         (             [1] => array                 (                     [id] => 1                     [parent_id] => 0                     [name] =>                 )              [3] => array                 (                     [id] => 3                     [parent_id] => 1                     [name] => person 2                 )              [2] => array                 (                     [id] => 2                     [parent_id] => 1                     [name] => person 1                 )              [5] => array                 (                     [id] => 5                     [parent_id] => 1                     [name] => gallery                 )              [4] => array                 (                     [id] => 4                     [parent_id] => 1                     [name] => cv                 )              [6] => array                 (                     [id] => 6                     [parent_id] => 0                     [name] => contact                 )              [8] => array                 (                     [id] => 8                     [parent_id] => 6                     [name] => contactinfo                 )              [7] => array                 (                     [id] => 7                     [parent_id] => 6                     [name] => pictures                 )          )      [parents] => array         (             [0] => array                 (                     [0] => 1                     [1] => 6                 )              [1] => array                 (                     [0] => 3                     [1] => 2                     [2] => 5                     [3] => 4                 )              [6] => array                 (                     [0] => 8                     [1] => 7                 )          )  ) 

and there's function use render data drop-down menu:

// render drop-down menu echo buildmenu(0, $hierarchy)   function buildmenu($parentid, array $menudata) {     $html = '';       if (isset($menudata['parents'][$parentid])) {          $html = '<ul>';           foreach ($menudata['parents'][$parentid] $itemid) {               $html .= '<li><a href="#">' . $menudata['items'][$itemid]['name'] . '</a>';               // find childitems recursively              $html .= buildmenu($itemid, $menudata);               $html .= '</li>';          }          $html .= '</ul>';      }      return $html;  } 

this working expected. right want to turn drop down selectbox, output like

<select>    <option value="id1">parent</option>   <option value="id2">- child 1</option>   <option value="id2">-- sub-child 1</option>  </select> 

how can that?

by modifying current function following

 function buildmenu($parentid, array $menudata) {      $html = '';      //static variable, shared across (recursive in context) calls of function      static $level = 1;       if(isset($menudata['parents'][$parentid])) {          if($level == 1)             $html .= '<select>';          foreach($menudata['parents'][$parentid] $itemid) {             $html .= '<option value="__value_here__">' . str_repeat('--', $level - 1) . $menudata['items'][$itemid]['name'] . '</option>';             //subsequent items indented 1 level            $level++;             $html .= buildmenu($itemid, $menudata);             //recursive call has returned, restore level            $level--;         }          if($level == 1)            $html .= '</select>';      }       return $html;  } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -