Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:使用select的复杂嵌套循环_Php_Database_Loops_For Loop_Foreach - Fatal编程技术网

PHP:使用select的复杂嵌套循环

PHP:使用select的复杂嵌套循环,php,database,loops,for-loop,foreach,Php,Database,Loops,For Loop,Foreach,我目前的项目有问题 我的数据库中有: ID---FOLDERID----描述 1---0---=>根文件夹(无法删除) 2----1---=>第一个文件夹(对根文件夹的引用) 3----1---=>第二个文件夹(也引用根目录) 4----2---=>第一个文件夹中的子文件夹 5----4---=>子文件夹中的子文件夹 这将如下所示: 第一文件夹 子文件夹 子文件夹中的子文件夹 第二文件夹 好的,当我想删除第一个文件夹时,我也想删除所有的子文件夹 我该怎么办 我知道我需要一个带计数器的循

我目前的项目有问题

我的数据库中有:

ID---FOLDERID----描述

1---0---=>根文件夹(无法删除)

2----1---=>第一个文件夹(对根文件夹的引用)

3----1---=>第二个文件夹(也引用根目录)

4----2---=>第一个文件夹中的子文件夹

5----4---=>子文件夹中的子文件夹

这将如下所示:

  • 第一文件夹
  • 子文件夹
    • 子文件夹中的子文件夹
  • 第二文件夹
  • 好的,当我想删除第一个文件夹时,我也想删除所有的子文件夹

    我该怎么办

    我知道我需要一个带计数器的循环,但我不知道怎么做

    这就是我所拥有的:但仅适用于3个文件夹:

         $del_id = [];
    
        do {
            $count = \DB::table('files')
                ->select('id', 'foldersID')
                ->where('userID', '=', \Auth::user()->id)
                ->where('foldersID', '=', $id)
                ->count();
    
            //count = 1
    
            if ($count == 0) {
    
                \DB::table('files')
                    ->where('userID', '=', \Auth::user()->id)
                    ->where('id', '=', $id)
                    ->delete();
    
            } else {
                //hier
                $_id = $id; //2
    
                for ($i = 0; $i <= $count; $i++) { //1 mal
    
                    $_files = \DB::table('files')
                        ->select('id')
                        ->where('userID', '=', \Auth::user()->id)
                        ->where('foldersID', '=', $_id)
                        ->get();
    
                    //3
    
                    $files = json_decode($_files, true);
    
                    //return print_r($files);
                    // return count($files);
                    for ($i = 0; $i <= count($files) - 1; $i++) {
                        array_push($del_id, $files[$i]);
                    }
                   //3 && 4
                }
    
                for ($i = 0; $i <= count($del_id) - 1; $i++) {
                    $_files = \DB::table('files')
                        ->select('id')
                        ->where('userID', '=', \Auth::user()->id)
                        ->where('foldersID', '=', $del_id[$i])
                        ->get();
    
                    $files = json_decode($_files, true);
                    return $files;
    
                    /*   \DB::table('files')
                        ->where('userID', '=', \Auth::user()->id)
                        ->where('id', '=', $del_id[$i])
                        ->delete();
                */
                 }
    
                \DB::table('files')
                    ->where('userID', '=', \Auth::user()->id)
                    ->where('id', '=', $id)
                    ->delete();
    
            }
        } while ($count >= 1);
    
    $del_id=[];
    做{
    $count=\DB::表('文件')
    ->选择('id','foldersID')
    ->其中('userID','=',\Auth::user()->id)
    ->其中('foldersID','=',$id)
    ->计数();
    //计数=1
    如果($count==0){
    \DB::表('文件')
    ->其中('userID','=',\Auth::user()->id)
    ->其中('id','=',$id)
    ->删除();
    }否则{
    //希尔
    $\u id=$id;//2
    对于($i=0;$i选择('id'))
    ->其中('userID','=',\Auth::user()->id)
    ->其中('foldersID','=',$\u id)
    ->get();
    //3
    $files=json_解码($_文件,true);
    //返回打印文件($files);
    //返回计数($文件);
    对于($i=0;$i其中('userID','=',\Auth::user()->id)
    ->其中('foldersID','=',$del_id[$i])
    ->get();
    $files=json_解码($_文件,true);
    返回$files;
    /*\DB::表('文件')
    ->其中('userID','=',\Auth::user()->id)
    ->其中('id','=',$del_id[$i])
    ->删除();
    */
    }
    \DB::表('文件')
    ->其中('userID','=',\Auth::user()->id)
    ->其中('id','=',$id)
    ->删除();
    }
    }而($count>=1);
    
    有人能帮我吗


    提前感谢。

    您需要使用递归来完成此操作。基本上,创建一个函数来删除文件夹。调用该函数时,它会检查是否有子文件夹,如果有,它会使用子文件夹的id调用自己

    下面我介绍了用于测试该方法的SQL以及显示递归方法的PHP脚本

    SQL创建“测试”数据库 使用递归的PHP脚本 运行我们的脚本:

    root@zim:~/testing# php db_recursion.php
    found sub folder with id: 4 - recursing
    found sub folder with id: 5 - recursing
    deleting folder with id: 5 - success
    deleting folder with id: 4 - success
    deleting folder with id: 2 - success
    
    检查表中现在的内容:

    root@zim:~/testing# mysql -p -e 'select * from files' testing;
    Enter password:
    +----+----------+---------------------+
    | id | folderid | description         |
    +----+----------+---------------------+
    |  1 |        0 | root folder         |
    |  2 |        1 | first folder        |
    |  3 |        1 | second folder       |
    |  4 |        2 | sub of first folder |
    |  5 |        4 | sub of sub folder   |
    +----+----------+---------------------+
    
    root@zim:~/testing# mysql -p -e 'select * from files' testing;
    Enter password:
    +----+----------+---------------+
    | id | folderid | description   |
    +----+----------+---------------+
    |  1 |        0 | root folder   |
    |  3 |        1 | second folder |
    +----+----------+---------------+
    

    另外,我可能应该提到,如果您有100多个嵌套文件夹,您将遇到运行时错误。这可能会增加:非常感谢您,您帮助了我很多!我对递归一无所知。但现在对我来说这是有意义的。明天晚上(德国时间),我将在您的解决方案中发布我的代码。
    root@zim:~/testing# mysql -p -e 'select * from files' testing;
    Enter password:
    +----+----------+---------------------+
    | id | folderid | description         |
    +----+----------+---------------------+
    |  1 |        0 | root folder         |
    |  2 |        1 | first folder        |
    |  3 |        1 | second folder       |
    |  4 |        2 | sub of first folder |
    |  5 |        4 | sub of sub folder   |
    +----+----------+---------------------+
    
    root@zim:~/testing# php db_recursion.php
    found sub folder with id: 4 - recursing
    found sub folder with id: 5 - recursing
    deleting folder with id: 5 - success
    deleting folder with id: 4 - success
    deleting folder with id: 2 - success
    
    root@zim:~/testing# mysql -p -e 'select * from files' testing;
    Enter password:
    +----+----------+---------------+
    | id | folderid | description   |
    +----+----------+---------------+
    |  1 |        0 | root folder   |
    |  3 |        1 | second folder |
    +----+----------+---------------+