使用mysql显示文件夹结构

时间:2017-02-02 10:34:18

标签: php mysql

我需要使用mysql查询列出文件夹名称和子文件夹名称。子文件夹的深度未知。这是父文件夹可能有n级子文件夹。请参阅下面的示例文件夹结构:

-PHP
  -Developer
      -Junior
      -Senior
  -Tester
-Java
  -Test Engineer
-Android

文件夹结构可能会有所不同..

我的表看起来像

enter image description here

我想在父类及其子方式等选择框中加载文件夹名称。

  PHP
  Developer
  Junior
  Senior
  Tester
  Java
  Test Engineer
  Android

我尝试了什么?

$fOption="";
$parentFolders = $db->query("SELECT * FROM jp_emp_folder WHERE parent_id='0' AND emp_id='".$empId."'");
while($parentRows = $db->fetch_array($parentFolders)){
    $fOption .= "<option value='".$parentRows['id']."'>".$parentRows['folder_name']."</option>";
    $child = $db->query("SELECT * FROM jp_emp_folder WHERE parent_id='".$parentRows['id']."' AND emp_id='".$empId."'");
    while($childRows = $db->fetch_array($child)){
        $fOption .= "<option value='".$childRows['id']."'>".$childRows['folder_name']."</option>";
    }
} 
echo $fOption;

我知道这只提供2级文件夹结构。但我有n级。我该如何实现这一目标?请建议..

2 个答案:

答案 0 :(得分:1)

您可以使用此方法构建层次结构。

第1步:使用PHP构建$ data

$data = array();
$results = $db->query("SELECT * FROM jp_emp_folder WHERE emp_id=" . (int)$empId);
while($rows = $db->fetch_array($results)) {
  $data[$row['id']] = $rows;
}

步骤2:递归读取$ data以构建层次结构

function readData($parent_id = 0) {
        global $data; // I'm just lazy, avoid using "global"
        foreach($data as $id => $item) {
            if($parent_id > 0 && $item['parent_id'] != $parent_id) {
                continue;
            }
            echo '<option value="' . $id . '">' . $item['folder_name'] . '</option>' . PHP_EOL;
            unset($data[$id]); // Remove echo-ed contents
            if($item['parent_id'] > 0) {
                readData((int)$id);
            }
        }
    }

readData(); // Start reading $data

将输出以下内容:

<option value="1">PHP</option>
<option value="2">Java</option>
<option value="3">Android</option>
<option value="4">Test Engineer</option>
<option value="5">Developer</option>
<option value="7">Junior</option>
<option value="6">Tester</option>

答案 1 :(得分:0)

检查一下。

if

输出

$all_folders = $db->query("SELECT * FROM jp_emp_folder WHERE emp_id='".$empId."'");
$array_column = array_column($all_folders, 'parent_id');        
$all_parent0_dir = array_keys($array_column, "0");  //get all base folders

foreach ($all_parent0_dir as $value) { 
    $dir = $all_folders[$value];
    echo '<option value="'.$dir['id'].'">'.$dir['foler_name'].'</option>';
    $parent_id = $dir['id'];
    $nodeNumber = 1;
    $lastElem = FALSE;//is this last element of the folder
    $nodeArray = [];//save all the nodes of the folder
    while ( $parent_id != $dir['id'] || $lastElem == FALSE) {
        /*************************************************************/
        //get details of a specific folder
        $last = 1;
        $sub_dir_ids = array_keys($array_column, $parent_id); 
        if($sub_dir_ids)
        { 
            $sub_dir = isset($sub_dir_ids[$nodeNumber-1])? $sub_dir_ids[$nodeNumber-1] : null;
            if($sub_dir){ 
                $res = $all_folders[$sub_dir];
                echo '<option value="'.$res['id'].'">'.$res['foler_name'].'</option>';
                if($res){$last = 0; }                       
            }
        }
        /*************************************************************/
        if($last == 1){           
            if($parent_id != $dir['id']){//if $parent_id didn't reach the first element (starting point)
                $lastElement = array_pop($nodeArray);
                $parent_id = $lastElement[0];
                $nodeNumber = $lastElement[1] + 1;
            }else{$lastElem = TRUE;}
        }else{
           $lastElem = FALSE;
           array_push($nodeArray, array($parent_id,$nodeNumber));
           $parent_id = $res['id'];//$parent_id for inner folder
           $nodeNumber = 1;//go to next step - inner folder
        }
    }            
}