2011-10-18 117 views
-1

只是爲了確認@comments:這不是字面上的文件夾,他們正在使用的對象表示,在同一個文件夾中,由MySQL已知的所有存在。使用別人的代碼 - 我會用一個真正的文檔樹。PHP遞歸文件夾計數(OOP,不是字面文件夾)

編輯2:夥計們,基本上,文件存儲在某個地方,但他們的位置並不重要。爲了使管理更好,並使文檔與文檔相關聯,有幾個類DocumentCategory就是它的原始文件夾。我需要統計文件夾的全部文件,包括子文件夾,以便我不顯示空樹。

我試圖寫一個遞歸函數,其對一個文件夾,它可以有子文件夾裏面的文件。這裏是我到目前爲止,但我不知道我怎麼能保持數:

function recursiveCount($id, $access_permission, $count) { 
    global $db; 
    $count = 0; 
    $count += Document::GetAll($id, $access_permission); 

    $docCats = DocumentCategory::GetAll($id); 
    if(!empty($docCats)) { 
     foreach($docCats as $docCat) { 
      // what to do recursively 
      // i need to call this function again 
     } 
    } 
} 
+0

檢查這裏http://php.net/manual/de/function的意見。 readdir.php – Mob

+0

可能重複[PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator檢索完整樹](http://stackoverflow.com/questions/2418068/php-spl-recursivedirectoryiterator-recursiveiteratoriterator-retrieving-the-full) – Gordon

+0

請你澄清一下編輯你剛剛創建的?請顯示「文件夾」類。 MySql與它有什麼關係?爲什麼當他們都在同一個文件夾(文件夾類或實際文件夾)時需要遞歸? – Gordon

回答

0

如果你試圖做一個遞歸函數,進行遞歸調用。這不是完全針對您的案例編寫的,而是顯示該過程。

<?php 

$folders = array('folder1' => array 
         ('folder2' => array 
             ('folder3' => null, 
             'folder4' => array 
                ('folder5' => null 
                ) 
             ), 
          'folder6' => array 
             ('folder7' => null 
             ) 
         ), 
      'folder8' => array 
          ('folder9' => null, 
          'folder10' => array 
             ('folder11' => null, 
              'folder12' => null, 
              'folder13' => null 
             ) 
         ) 
      ); 

function countFolders($folders, $numFolders = 0) { 
    foreach ($folders as $folderName => $subFolders) { 

     // increment the number of folders we've encountered 
     $numFolders++; 

     // if this folder has subfolders... 
     // This might be a function call, such as 'hasSubFolders' 
     // or another check, such as an array key existing, or 
     // an object property existing or being set to 1 or more 
     // etc. 
     if (is_array($subFolders)) { 

      // count how many additional subfolders there are, 
      // passing in the current folder count, and updating 
      // our own copy with the new count 
      $numFolders = countFolders($subFolders, $numFolders); 
     } 
    } 
    // return the total number of folders 
    return $numFolders; 
} 

echo countFolders($folders), "\n"; 

將輸出:

13 

或者,你可以使用引用變量:

function countFolders($folders, &$numFolders = 0) { 
    foreach ($folders as $folderName => $subFolders) { 
     $numFolders++; 
     if (is_array($subFolders)) { 
      countFolders($subFolders, $numFolders /* passed as a reference */); 
     } 
    } 
    return $numFolders; 
} 

(請注意函數的參數列表中&

+1

鬱鬱蔥蔥,非常感謝你,參考是我需要找到的!傳說! –

0

讓我們假設你有文件和文件夾,每個文件都屬於一個文件夾,每個文件夾可能有一些文件nt文件夾。因此,有是在數據庫中的兩個表那樣:

files 
    id: int 
    folder_id: int 
    name: varchar(255) 

folders 
    id: int 
    parent: int 
    name: varchar(255) 

這情況下,我會寫這樣的代碼:

$files = $foldes = array(); 

$res = mysql_query("select * from folders"); 
while ($rw = @mysql_fetch_array($res)) { 
    $rw["children"] = $folders[$rw["id"]]["children"]; 
    $folders[$rw["id"]] = $rw; 
    $folders[$rw["parent"]]["children"][] = $rw["id"]; 
} 

$res = mysql_query("select * from files"); 
while ($rw = @mysql_fetch_array($res)) { 
    $files[$rw["id"]] = $rw; 
    $folders[$rw["folder_id"]]["inner_cnt"]++; 
} 

recursiveCount(0); 

function recursiveCount($root) { 
    GLOBAL $folders; 
    $folders[$root]["cnt"] = $folders[$root]["inner_cnt"]; 
    foreach($folders[$root]["children"] as $i) 
    $folders[$root]["cnt"] += recursiveCount($i); 
    return $folders[$root]["cnt"]; 
}