Welcome Guest, Not a member yet? Register   Sign In
Recursively delete files and folders from one MYSQL table?
#1

[eluser]Christophe28[/eluser]
Hello,

I have a large MYSQL table with files and folders (in one table) The files having a parent_folder_id indicating the parent folder id in the same table. Imagine a user delete a folder with 3 files and 1 folder.

The SQL would be something like:

"DELETE FROM items WHERE parent_folder_id = $parent_folder_id";

Great, but the problem is that I don't know if the folder within the deleted folder also has files and maybe even more folders with files to delete and so on ...

So I was thinking to write a script which go check if the folder has any more folders and if so, get the folder_id and check again in the table ...

At the end I need one array with all the ids of the items (files and folders) to delete at once in the table.

Something like:
Code:
// $items = array with ids of deleted items

$results = $this->items_model->get_id_type($items);
                
    if ( !empty($results) ) {

        $ids = array();
                    
        foreach ( $results as $result ) {
            
            $ids[] = $result->id;

            if ( $result->type == 'folder' ) {

                // If there is a folder, go check the table again
                // And check again if there is a folder in the results

            }
            
        }
        
        print_r($ids);
    }
Is there a special technique to do this? Using a do-while or something like that?

Thanks in advance!

Christophe
#2

[eluser]Christophe28[/eluser]
I have found this article on MySQL: http://dev.mysql.com/tech-resources/arti...-data.html

Seems exactly what I need! :-)




Theme © iAndrew 2016 - Forum software by © MyBB