<?php
define('IN_ECS','admin');
include('../includes/init.php');
$act = $_REQUEST['act'];
// 对无限级分类数组进行递归排序
function rec($arr,$id,$lev=0) {
static $list = array();
foreach($arr as $k=>$v) {
if($v['parent_id'] == $id) {
$v['lev'] = $lev;
$list[] = $v;
rec($arr,$v['cat_id'],$lev+1);
}
}
return $list;
}
// 找一个栏目的家谱树
function tree($arr,$id) {
static $list = array();
foreach($arr as $v) {
if($v['cat_id'] == $id) {
$list[] = $v;
if($v['parent_id'] > 0) {
tree($arr,$v['parent_id']);
}
}
}
return $list;
}
if($act == '') {
$act = 'list';
}
if($act == 'list') {
$sql = 'select cat_id,cat_name,parent_id from category';
$catlist = $db->getAll($sql);
$catlist = rec($catlist,0);
$smarty->assign('catlist',$catlist);
$smarty->display('catelist.html');
}
if($act == 'add') {
$sql = 'select cat_id,cat_name,parent_id from category';
$catlist = $db->getAll($sql);
$catlist = rec($catlist,0);
$smarty->assign('clist',$catlist);
$smarty->display('cateadd.html');
}
if($act == 'insert') {
// 做一些数据合法性的检验,略过
$cat = array();
$cat['cat_name'] = trim($_POST['cat_name']);
$cat['parent_id'] = $_POST['parent_id'] + 0;
$cat['keywords'] = trim($_POST['keywords']);
$cat['cat_desc'] = trim($_POST['cat_desc']);
// 第1个参数是表名,第2个数组数据, 第3个代表动作,insert ,update,默认是insert
$cat_id = $_POST['cat_id'] + 0;
// 判断 栏目的新父id 是不是此栏目的子孙
$sql = 'select cat_id,parent_id from category';
$list = $db->getAll($sql);
$list = tree($list,$cat['parent_id']);
foreach($list as $v) {
if($v['cat_id'] == $cat_id) {
echo "不能以自身或子孙栏目做自己的父栏目";
exit;
}
}
if(!$cat_id) {
if($db->autoExecute('category',$cat)) {
sys_msg("添加栏目成功",'cate.php?act=list');
} else {
sys_msg('添加栏目失败');
}
} else {
if($db->autoExecute('category',$cat,'update',' cat_id='.$cat_id)) {
sys_msg("修改栏目成功",'cate.php?act=list');
} else {
sys_msg('修改栏目失败');
}
}
}
if($act == 'edit') {
$cat_id = $_GET['cat_id'] + 0;
$sql = 'select * from category where cat_id = '.$cat_id;
$cat = $db->getRow($sql);
$sql = 'select cat_id,cat_name,parent_id from category';
$catlist = $db->getAll($sql);
$catlist = rec($catlist,0);
/*foreach($catlist as &$v) {
$v['cat_name'] = str_repeat(' ',$v['lev']).$v['cat_name'];
}*/
$smarty->assign('clist',$catlist);
$smarty->assign('info',$cat);
$smarty->display('catedit.html');
}
ECShop无限级分类
最新推荐文章于 2021-03-20 17:32:02 发布