第一步:为数据库中的ecs_category表增加一个字段,用来存储上传上来的分类图片名,这步可以直接在后台执行sql语句:
alter table ecs_category add thumb varchar(255) not null default '';
注意,如果你的ecshop表前缀不是ecs_请把ecs_换成你自已的表前缀
第二步:在admin/templates/categroy_info.html增加上传要类图片的表单:
代码如下
第三步:修改category.php的insert和update
在顶部增加:
代码如下define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
/***新增加的开始***/
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
/***新增加的结束***/
$exc = new exchange($ecs->table("category"), $db, 'cat_id', 'cat_name');
在insert段代码
/*------------------------------------------------------ */
//-- 商品分类添加时的处理
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'insert')
{
/* 权限检查 */
admin_priv('cat_manage');
/* 初始化变量 */
$cat['cat_id'] = !empty($_POST['cat_id']) ? intval($_POST['cat_id']) : 0;
$cat['parent_id'] = !empty($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
$cat['sort_order'] = !empty($_POST['sort_order']) ? intval($_POST['sort_order']) : 0;
$cat['keywords'] = !empty($_POST['keywords']) ? trim($_POST['keywords']) : '';
$cat['cat_desc'] = !empty($_POST['cat_desc']) ? $_POST['cat_desc'] : '';
$cat['measure_unit'] = !empty($_POST['measure_unit']) ? trim($_POST['measure_unit']) : '';
$cat['cat_name'] = !empty($_POST['cat_name']) ? trim($_POST['cat_name']) : '';
$cat['show_in_nav'] = !empty($_POST['show_in_nav']) ? intval($_POST['show_in_nav']): 0;
$cat['style'] = !empty($_POST['style']) ? trim($_POST['style']) : '';
$cat['is_show'] = !empty($_POST['is_show']) ? intval($_POST['is_show']) : 0;
$cat['grade'] = !empty($_POST['grade']) ? intval($_POST['grade']) : 0;
$cat['filter_attr'] = !empty($_POST['filter_attr']) ? implode(',', array_unique($_POST['filter_attr'])) : 0;
$cat['cat_recommend'] = !empty($_POST['cat_recommend']) ? $_POST['cat_recommend'] : array();
if (cat_exists($cat['cat_name'], $cat['parent_id']))
{
/* 同级别下不能有重复的分类名称 */
$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
sys_msg($_LANG['catname_exist'], 0, $link);
}
if($cat['grade'] > 10 || $cat['grade'] < 0)
{
/* 价格区间数超过范围 */
$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
sys_msg($_LANG['grade_error'], 0, $link);
}
/*处理图片*/
$img_name = basename($image->upload_image($_FILES['thumb'],'catthumb'));
if($img_name) $cat['thumb'] = $img_name;
/* 入库的操作 */
if ($db->autoExecute($ecs->table('category'), $cat) !== false)
{
$cat_id = $db->insert_id();
if($cat['show_in_nav'] == 1)
{
$vieworder = $db->getOne("SELECT max(vieworder) FROM ". $ecs->table('nav') . " WHERE type = 'middle'");
$vieworder += 2;
//显示在自定义导航栏中
$sql = "INSERT INTO " . $ecs->table('nav') .
" (name,ctype,cid,ifshow,vieworder,opennew,url,type)".
" VALUES('" . $cat['cat_name'] . "', 'c', '".$db->insert_id()."','1','$vieworder','0', '" . build_uri('category', array('cid'=> $cat_id), $cat['cat_name']) . "','middle')";
$db->query($sql);
}
insert_cat_recommend($cat['cat_recommend'], $cat_id);
admin_log($_POST['cat_name'], 'add', 'category'); // 记录管理员操作
clear_cache_files(); // 清除缓存
/*添加链接*/
$link[0]['text'] = $_LANG['continue_add'];
$link[0]['href'] = 'category.php?act=add';
$link[1]['text'] = $_LANG['back_list'];
$link[1]['href'] = 'category.php?act=list';
sys_msg($_LANG['catadd_succed'], 0, $link);
}
}
在update段代码
/*------------------------------------------------------ */
//-- 编辑商品分类信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'update')
{
/* 权限检查 */
admin_priv('cat_manage');
/* 初始化变量 */
$cat_id = !empty($_POST['cat_id']) ? intval($_POST['cat_id']) : 0;
$old_cat_name = $_POST['old_cat_name'];
$cat['parent_id'] = !empty($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
$cat['sort_order'] = !empty($_POST['sort_order']) ? intval($_POST['sort_order']) : 0;
$cat['keywords'] = !empty($_POST['keywords']) ? trim($_POST['keywords']) : '';
$cat['cat_desc'] = !empty($_POST['cat_desc']) ? $_POST['cat_desc'] : '';
$cat['measure_unit'] = !empty($_POST['measure_unit']) ? trim($_POST['measure_unit']) : '';
$cat['cat_name'] = !empty($_POST['cat_name']) ? trim($_POST['cat_name']) : '';
$cat['is_show'] = !empty($_POST['is_show']) ? intval($_POST['is_show']) : 0;
$cat['show_in_nav'] = !empty($_POST['show_in_nav']) ? intval($_POST['show_in_nav']): 0;
$cat['style'] = !empty($_POST['style']) ? trim($_POST['style']) : '';
$cat['grade'] = !empty($_POST['grade']) ? intval($_POST['grade']) : 0;
$cat['filter_attr'] = !empty($_POST['filter_attr']) ? implode(',', array_unique($_POST['filter_attr'])) : 0;
$cat['cat_recommend'] = !empty($_POST['cat_recommend']) ? $_POST['cat_recommend'] : array();
/* 判断分类名是否重复 */
if ($cat['cat_name'] != $old_cat_name)
{
if (cat_exists($cat['cat_name'],$cat['parent_id'], $cat_id))
{
$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
sys_msg($_LANG['catname_exist'], 0, $link);
}
}
/* 判断上级目录是否合法 */
$children = array_keys(cat_list($cat_id, 0, false)); // 获得当前分类的所有下级分类
if (in_array($cat['parent_id'], $children))
{
/* 选定的父类是当前分类或当前分类的下级分类 */
$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
sys_msg($_LANG["is_leaf_error"], 0, $link);
}
if($cat['grade'] > 10 || $cat['grade'] < 0)
{
/* 价格区间数超过范围 */
$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
sys_msg($_LANG['grade_error'], 0, $link);
}
$dat = $db->getRow("SELECT cat_name, show_in_nav FROM ". $ecs->table('category') . " WHERE cat_id = '$cat_id'");
/* 处理图片 */
$img_name = basename($image->upload_image($_FILES['thumb'],'catthumb'));
if($img_name) $cat['thumb'] = $img_name;
if ($db->autoExecute($ecs->table('category'), $cat, 'UPDATE', "cat_id='$cat_id'"))
{
if($cat['cat_name'] != $dat['cat_name'])
{
//如果分类名称发生了改变
$sql = "UPDATE " . $ecs->table('nav') . " SET name = '" . $cat['cat_name'] . "' WHERE ctype = 'c' AND cid = '" . $cat_id . "' AND type = 'middle'";
$db->query($sql);
}
if($cat['show_in_nav'] != $dat['show_in_nav'])
{
//是否显示于导航栏发生了变化
if($cat['show_in_nav'] == 1)
{
//显示
$nid = $db->getOne("SELECT id FROM ". $ecs->table('nav') . " WHERE ctype = 'c' AND cid = '" . $cat_id . "' AND type = 'middle'");
if(empty($nid))
{
//不存在
$vieworder = $db->getOne("SELECT max(vieworder) FROM ". $ecs->table('nav') . " WHERE type = 'middle'");
$vieworder += 2;
$uri = build_uri('category', array('cid'=> $cat_id), $cat['cat_name']);
$sql = "INSERT INTO " . $ecs->table('nav') . " (name,ctype,cid,ifshow,vieworder,opennew,url,type) VALUES('" . $cat['cat_name'] . "', 'c', '$cat_id','1','$vieworder','0', '" . $uri . "','middle')";
}
else
{
$sql = "UPDATE " . $ecs->table('nav') . " SET ifshow = 1 WHERE ctype = 'c' AND cid = '" . $cat_id . "' AND type = 'middle'";
}
$db->query($sql);
}
else
{
//去除
$db->query("UPDATE " . $ecs->table('nav') . " SET ifshow = 0 WHERE ctype = 'c' AND cid = '" . $cat_id . "' AND type = 'middle'");
}
}
//更新首页推荐
insert_cat_recommend($cat['cat_recommend'], $cat_id);
/* 更新分类信息成功 */
clear_cache_files(); // 清除缓存
admin_log($_POST['cat_name'], 'edit', 'category'); // 记录管理员操作
/* 提示信息 */
$link[] = array('text' => $_LANG['back_list'], 'href' => 'category.php?act=list');
sys_msg($_LANG['catedit_succed'], 0, $link);
}
}
然后再增加一个删除分类图片的方法
/*------------------------------------------------------ */
//-- 删除品牌图片
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'drop_thumb')
{
/* 权限判断 */
admin_priv('cat_manage');
$cat_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
/* 取得logo名称 */
$sql = "SELECT thumb FROM " .$ecs->table('category'). " WHERE cat_id = '$cat_id'";
$thumb_name = $db->getOne($sql);
if (!empty($thumb_name))
{
@unlink(ROOT_PATH . DATA_DIR . '/catthumb/' .$thumb_name);
$sql = "UPDATE " .$ecs->table('category'). " SET thumb = '' WHERE cat_id = '$cat_id'";
$db->query($sql);
}
$link= array(array('text' => '编辑商品分类', 'href' => 'category.php?act=edit&cat_id=' . $cat_id), array('text' =>'商品分类列表', 'href' => 'category.php?act=list'));
sys_msg('删除商品分类小图成功', 0, $link);
}