前文见---帝国cms7.2后台信息列表页批量添加tags http://www.zhongyf.com/qita/rj/2015-02-19/91911.html 对于其中的主要函数,jiuhecai做了改进,使得逻辑清晰,执行效率更高。 尤其是其中针对mysql的操作jiuhecai使用了比较少用但是高效的mysql函数,值得收藏学习。 - function eInsertTags2($tags,$classid,$idr,$newstime){
- global $empire,$dbtbpre,$class_r;
- //参数检查
- $tagname = RepPostVar($tags);
- if(!trim($tagname))printerror("TAGS信息不能为空", "", 1, 0, 1);
- $tag = explode(",", $tagname);
- if(count($tag)>1)printerror("只能添加一个TAGS词", "", 1, 0, 1);
- $count = count($idr); //统计ID数量
- if(empty($count))printerror("未选择信息ID", "", 1, 0, 1);
-
- $classid=(int)$classid;
- $mid=(int)$class_r[$classid][modid]; //获取模型id
- $tbname=$class_r[$classid][tbname];//获取表名
- //获取tagid
- $tagid=(int)$empire->gettotal("select tagid as total from {$dbtbpre}enewstags
- where tagname='$tagname'");
- if($tagid==0){ //新增tag
- $empire->query("insert into {$dbtbpre}enewstags(tagname,num,isgood,cid)
- values('$tagname',1,0,0);");
- $tagid=$empire->lastid();
- }else{ //已经存在的tag,过滤掉已经添加过的
- $ids=join(',',$idr);
- $allids=$empire->gettotal("select group_concat(id) as total
- from {$dbtbpre}enewstagsdata
- where
- tagid='$tagid'and id in($ids) and mid='$mid' ");
- $allidr=explode(',',$allids);
- $idr=array_diff($idr,$allidr);
- if(empty($idr))printerror("已经有了,无需重复添加", "", 1, 0, 1);
- }
- //开始正经干活
- foreach($idr as $id){
- //获得副表名称
- $jhcr = $empire->fetch1("select classid,stb,newstime from {$dbtbpre}ecms_".$tbname."
- where id='$id' limit 1");
- //更新副表
- $ftbname=$dbtbpre."ecms_".$tbname."_data_".$jhcr['stb'];
- $empire->query("update $ftbname set
- infotags=trim(BOTH ',' from concat(infotags,',','$tagname'))
- where id='$id'");
- //更新tags数据表
- $empire->query("insert into {$dbtbpre}enewstagsdata(tagid,classid,id,newstime,mid)
- values('$tagid','$jhcr[classid]','$id','$jhcr[newstime]','$mid')");
- }
- //更新tags主表
- $empire->query("update {$dbtbpre}enewstags set num=num+".count($idr)."
- where tagid='$tagid'");
- printerror("批量添加TAGS成功", "", 1, 0, 1);
- }
|