/*
* 迭代sql查询,生成无限级分类树
* 数据结构
CREATE TABLE `cat` (
`cid` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`cname` VARCHAR(50) NOT NULL,
`lev` INT(5) UNSIGNED NOT NULL COMMENT '分类层数level',
`pid` INT(5) UNSIGNED NOT NULL COMMENT '上级cid',
PRIMARY KEY (`cid`)
)
* 本程序仅供演示“目录树”的算法概念,未做参数有效性的校验
* 本程序含有添加、删除、列目录树功能,编辑功能未实现(非常简单啦,你们懂得)
*/
error_reporting(7);
header("charset=utf-8");
mysql_connect("localhost", "root", "root");
mysql_select_db("cat");
mysql_query("set names utf8");
$act = $_GET['act'];
if($act == "insert") {
$cname = trim($_POST['cname']);
$pid = intval($_POST['cid']);
//取得上级目录的分类层数
$query = "select * from cat where cid='$pid'";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$parent_lev = $row['lev'];
$query = "insert into cat set cname='$cname',lev='".($parent_lev+1)."',pid='$pid'";
mysql_query($query);
echo "";
die;
}elseif($act == "del") {
$cid = intval($_GET['cid']);
if(delCat($cid)) {
echo "";
die;
}
}elseif($act == "edit") {
$cid = intval($_GET['cid']);
$query = "select * from cat where cid='$cid'";
$res = mysql_query($query);
$editRow = mysql_fetch_assoc($res);
//将当前分类及其子分类的cid全部获得,在select中要屏蔽(disabled)
$pid = $editRow['cid'];
$subArr = getCat($pid);
//提取cid
$subCidArr = array();
foreach($subArr as $_row) {
$subCidArr[] = $_row['cid'];
}
array_unshift($subCidArr, $editRow['cid']);//所有子分类cid的数组头部压入当前编辑分类的cid,形成当前分类及其所属所有子分类的cid数组
$pid = 0;//从根分类开始列目录树
$okArr = getCat($pid);
?>
}elseif($act == "update") {
$cid = intval($_POST['edit_cid']);
$cname = trim($_POST['cname']);//新的分类名
$pid = intval($_POST['cid']);//新的父分类
//原始分类记录
$query = "select * from cat where cid='$cid'";
$res = mysql_query($query);
$cat_old_row = mysql_fetch_assoc($res);
//新的父分类记录
$query = "select * from cat where cid='$pid'";
$res = mysql_query($query);
$cat_p_row = mysql_fetch_assoc($res);
//比对原始记录的lev和将所属新父分类后的lev
$lev_old = $cat_old_row['lev'];
$lev_new = $cat_p_row['lev']+1;
$lev_diff = $lev_old - $lev_new;//调整后的lev差值
//获得所有子分类
$subArr = getCat($cid);
//提取所有子分类的cid
$subCidArr = array();
foreach($subArr as $_row) {
$subCidArr[] = $_row['cid'];
}
array_unshift($subCidArr, $cid);//所有子分类cid的数组头部压入当前编辑分类的cid,形成当前分类及其所属所有子分类的cid数组
//修改当前分类记录信息
$query1 = "update cat set cname='$cname', pid='$pid' where cid='$cid'";
mysql_query($query1);
//并当前分类及其所有子分类的lev做相应的差值调整
$query2 = "update cat set lev=lev-$lev_diff where cid in (".implode(",", $subCidArr).")";
mysql_query($query2);
echo "";
die;
}else{
/* 按照分类层次等级列目录树 */
$pid = 0;//从根分类开始列目录树
$catAllArr = getCat($pid);
echo "
目录树
";foreach($catAllArr as $row) {
for($i=1;$i<$row['lev'];$i++) {
echo " ";
}
echo $row['cname']." 编辑 删除
";
}
echo "
";
echo "
创建新分类
";?>
}
//递归获得分类目录树
function getCat($pid, &$arr="") {
if(empty($arr)) {
$arr = array();
}
$sql = "select * from cat where pid='$pid'";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
if(!empty($row)) {
$arr[] = $row;
getCat($row['cid'], $arr);
}
}
return $arr;
}
//递归删除分类目录树
function delCat($cid) {
//获得下一层分类
$sql = "select * from cat where pid='$cid'";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
if(!empty($row)) {
delCat($row['cid']);
}
}
//删除本层分类
$sql = "delete from cat where cid='$cid'";
$res = mysql_query($sql);
return $res;
}