ECShop无限级分类

<?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');
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值