PHP 递归生成 option

<?php
/**
 *
 * $parent_id 父id,用于获取每级数据
 * $selected_id 当前需要选中的option
 */
function tree_options($parent_id = 0, $selected_id = 0)
{
    $options = '';

    $where = array(
        'parent_id' => $parent_id,
    );
    $data_list = "这里获取数据库结果集";
    if ($data_list) {
        foreach ($data_list as $data) {
            $selected = '';
            if ($selected_id == $data['id']) {
                $selected = 'selected';
            }

            $options .= "<option ".$selected." value=".$data['id'].""; $options .= ">".str_repeat('  ', $data['tree_level']*3).'>'.$data['option_name']."";
            $options .= $this->tree_options($data['id'], $selected_id);
        }
    }

    return $options;
}
?>

 

PHP递归创建目录

<?php
// 递归创建目录
function recursive_mkdir($created_path, $before_path, $mode = 0777) {
     $dirs = explode('/' , $created_path);
     $count = count($dirs);
     for ($i = 0; $i < $count; ++$i) {
         if (empty($dirs[$i])) continue;
         
         $before_path .= $dirs[$i]."/";
         if (!is_dir($before_path) && !mkdir($before_path, $mode)) {
             continue;
         }
     }
}
?>