Skip to content

Instantly share code, notes, and snippets.

@natsu90
Created March 3, 2015 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natsu90/6868a1eececbfbe020de to your computer and use it in GitHub Desktop.
Save natsu90/6868a1eececbfbe020de to your computer and use it in GitHub Desktop.
recursive func for dropdown (php)
$folders = $this->files_model->select('id,file_parent_id,file_display_name,file_type')->where(array('file_type' => 'folder'))->find_all();
$indent = array();
$get_menu = function($folders, $folder_id = 0) use(&$get_menu, &$indent) {
$menu_html = '';
foreach($folders as $folder)
{
if($folder->file_parent_id == $folder_id) {
if($folder_id > 0)
$indent[$folder->id] = $indent[$folder->file_parent_id]+1;
else
$indent[$folder->id] = 0;
$menu_html .= '<option value="'.$folder->id.'">';
for($i = 0; $i < $indent[$folder->id]; $i++)
{
$menu_html .= '---';
}
$menu_html .= $folder->file_display_name.'</option>';
$menu_html .= $get_menu($folders, $folder->id);
}
}
return $menu_html;
};
echo '<select>'.$get_menu($folders).'</select>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment