0

我在网站上有画廊,我需要删除所有文件夹

我的结构数据库:(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])          
  } 

}

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

4

1 回答 1

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);
于 2012-10-26T11:15:00.197 回答