1

有什么办法可以避免这样做(这是循环访问数据库中的目录数据,然后为它的所有孩子和孩子的孩子获取相同的数据......无限期:

$directories = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `level` = '1' AND `batch` = %s", $lastBatchID);
foreach ($directories as $dir) {

$dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir");
$size = intval($dirInfo[0]['size']) * 1024;
echo stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";


$directories2 = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `parentID` = %s AND `batch` = %s", $dir, $lastBatchID);

foreach ($directories2 as $dir2) {

    $dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir2");
    $size = intval($dirInfo[0]['size']) * 1024;
    echo "=>" . stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";


    $directories3 = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `parentID` = %s AND `batch` = %s", $dir2, $lastBatchID);

    foreach ($directories3 as $dir3) {

        $dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir3");
        $size = intval($dirInfo[0]['size']) * 1024;
        echo "=>=>" . stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";


        $directories4 = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `parentID` = %s AND `batch` = %s", $dir3, $lastBatchID);

        foreach ($directories4 as $dir4) {

            $dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir4");
            $size = intval($dirInfo[0]['size']) * 1024;
            echo "=>=>=>" . stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";

            $directories5 = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `parentID` = %s AND `batch` = %s", $dir4, $lastBatchID);

            foreach ($directories5 as $dir5) {

                $dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir5");
                $size = intval($dirInfo[0]['size']) * 1024;
                echo "=>=>=>=>" . stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";
            }
        }
    }
}

}

基本上,我们正在获取 x 级目录的数据,并且需要为每个级别循环显示数据,如下所示:

C:\it\AD-Chris Integration -Current:49.70M
=>C:\it\AD-Chris Integration\AD-Chris2 -Current:49.69M
=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris -Current:5.22M
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Common -Current:599.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Department -Current:453.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Encrypt -Current:327.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Location -Current:387.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Manager -Current:1.36M
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\ScanForStaffID -Current:348.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\Title -Current:498.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\bin -Current:529.00K
=>=>=>C:\it\AD-Chris Integration\AD-Chris2\AD-Chris\obj -Current:439.00K
=>=>C:\it\AD-Chris Integration\AD-Chris2\_Deploy -Current:622.00K

我有一个可用的变量,其中包含我们需要深入的级别数

4

1 回答 1

3
function something($dir, $lastBatchID, $level = 0) {
    $dirInfo = DB::query("SELECT `folder`, `size` FROM `data` WHERE `id` = $dir");
    $size = intval($dirInfo[0]['size']) * 1024;

    echo str_repeat('=>', $level);
    echo stripslashes($dirInfo[0]['folder']) . "  -Current:" . human_filesize($size) . "<br>";

    $directories2 = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `parentID` = %s AND `batch` = %s", $dir, $lastBatchID);

    if ($level > 5) return;

    foreach ($directories2 as $dir2) {
        something($dir2, $lastBatchID, $level + 1);
    }
}

$directories = DB::queryOneColumn('id', "SELECT `id` FROM `data` WHERE `level` = '1' AND `batch` = %s", $lastBatchID);
foreach ($directories as $dir) {
    something($dir, $lastBatchID);
}
于 2013-01-27T23:11:44.010 回答