I have a problem at my current project.
I have this in my DB:
ID ---- FOLDERID ---- Description
1 ---- 0 ---- => Root Folder (can't be deleted)
2 ---- 1 ---- =>First Folder (references to Root Folder)
3 ---- 1 ---- => Second Folder (also references to Root)
4 ---- 2 ---- => Sub folder in First Folder
5 ---- 4 ---- => Sub folder in Sub Folder
this will be displayed like this:
- First Folder
- Sub Folder
- Sub Folder in Sub Folder
- Sub Folder
- Second Folder
Ok, So when i want to delete the First Folder, i want to delete all subfolders, too.
How can i handle this?
i know i need a loop with counter, but i don't know how to do it.
this is what i have: but only works for 3 folders:
$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);
Can anyone please help me?
Thanks in advance.