【发布时间】:2017-03-24 20:54:35
【问题描述】:
我当前的项目有问题。
我的数据库中有这个:
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);
谁能帮帮我?
提前致谢。
【问题讨论】:
标签: php database loops for-loop foreach