欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

删除无限分类并同时删除它下面的所有子分类的方法

程序员文章站 2022-10-06 17:24:37
复制代码 代码如下: $act = isset ($_get['act']) ? trim ($_get['act']) : "; if ($act == 'del') {...
复制代码 代码如下:

$act = isset ($_get['act']) ? trim ($_get['act']) : ";
if ($act == 'del')
{
$sort_id = isset ($_get['id']) ? intval($_get['id']) : '0' ;
$sort_ids = $sort_id;
$childrenids = getchildrenids ($sort_id);
if (!empty ($childrenids))
{
$sort_ids .= $childrenids;
}
$sql = “delete from `article_sort` where `sort_id` in ({$sort_ids})";
$res = mysql_query ($sql);
if ($res)
{
alert ('删除成功');
exit;
}
else
{
alert ('删除失败');
exit;
}
}

getchildrenids 这个函数以前已经给出来过,不清楚的请参考 自定义函数之获取无限分类id下的子类id集

自定义函数之获取无限分类id下的子类id集
复制代码 代码如下:

/*—————————————————— */
//– 获取无限分类id下面的子类id集
//– $sort_id = $sort_id.getchildrenids($sort_id);
//– $sql = " ….. where sort_id in ($sort_id)";
/*—————————————————— */
function getchildrenids ($sort_id)
{
global $db;
$ids = ";
$sql = "select * from ".$db->table('article_sort')." where `parent_id` = '{$sort_id}'";
$res = $db->query ($sql);
if ($res)
{
while ($row = $db->fetch_assoc ($res))
{
$ids .= ','.$row['sort_id'];
$ids .= getchildrenids ($row['sort_id']);
}
}
return $ids;
}