删除DB中的文件夹

时间:2012-10-26 10:39:27

标签: php sql algorithm

我在网站上有图库,我需要删除所有文件夹

我的结构DB :( g_folder)

id id_parent
1    0
2    0
3    2
4    2
5    3

例如

那么,我可以删除文件夹并删除所有父文件夹吗?

递归?

function delete(folder)
{
  count = select id from g_folder where id_parent = folder
  if(count == 0)
    deleteFolderById(folder)

  while(count != 0)
  {
     array = select id from g_folder where id_parent = folder
     for i in range(count)
     {
       deleteFolderById(array[i])
     }
     count = select id from g_folder where id_parent = folder
     delete(array[0])          
  } 

}

我的算法错了,请帮忙!谢谢大家!

1 个答案:

答案 0 :(得分:0)

试试这样:

function deletefolder($parentid)
{
    $query="select * from g_folder where parentid=".$parentid;
    $result=mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
        while($row=mysql_fetch_array($result))
        {
            deletefolder($row['id']);
        }
        $fdquery="delete from g_folder where parentid=".$parentid;
        $fdresult=mysql_query($fdquery);
    }

    $sdquery="delete from g_folder where id=".$parentid;
    $sdresult=mysql_query($sdquery);
}

deletefolder(2);