-1

Just to confirm @comments: this is NOT literal folders, they are represented using objects, and all exist in the same folder, known by MySQL. Working with someone else's code -- I would have used a true document tree.

Edit 2: Guys, basically, the documents are stored somewhere but their location doesn't matter. To make the admin nicer and have things associated with the documents, there's a few classes, DocumentCategory which is the folder as it were. I need to count a folders total files, including subfolders, so that I'm not displaying an empty tree.

I'm trying to write a recursive function which counts the files inside a folder, which can have subfolders. Here's what I have so far, but I'm not sure how I can keep the count:

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
        }
    }
}
Charles Kirk
  • 167
  • 1
  • 4
  • 14
  • Check the comments here http://php.net/manual/de/function.readdir.php – Mob Oct 18 '11 at 08:18
  • possible duplicate of [PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree](http://stackoverflow.com/questions/2418068/php-spl-recursivedirectoryiterator-recursiveiteratoriterator-retrieving-the-full) – Gordon Oct 18 '11 at 08:20
  • can you please clarify that edit you just made? Please show the "folder" class. What does MySql have to do with it? And why do you need recursion when they are all in the same folder (folder class or real folder)? – Gordon Oct 18 '11 at 08:24

2 Answers2

0

If you're trying to do a recursive function, make recursive calls. This isn't written for your case exactly, but shows the process.

<?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";

Would output:

13

Alternatively, you could use a reference variable:

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

(note the & in the function parameter list)

Daren Chandisingh
  • 2,157
  • 1
  • 13
  • 17
0

Lets assume You have files and folders, each file belongs to one folder, and each folder may have some parent folder. So there're two tables in database like that:

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

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

This case I would write such code:

$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"];
}        
Kasheftin
  • 7,509
  • 11
  • 41
  • 68