PHP头条
热点:

给TinkPHP添加一个更新的标签的方法


我们知道,thinkphp的拓展案例blog,只告诉我们怎样去添加标签tag,却没有删除和更新标签的方法,我在前面的《怎样彻底删除thinkphp案例blog的标签?》为拓展案例blog写了一个删除标签的方法,接下来将写一个标签的更新方法.

一般情况下,我们写博客后,很少去改动标签了,但是如果我们改动标签如,删除,添加,减少标签怎么办呢?这无疑造成think_tag和think_tagged两个表垃圾信息的积累,好了,言归正转.

在更新标签时我们来了解两个参数:

$oldtags:更新前,存在thinphp_tag表中标签

$newstags:更新时,插入thinphp_tag之前,表单提交过来的标签

更新文章时,标签可能会有以下几种变化:

1、$newstags与$oldtags部分相同-> 添加或减少或修改标签,标签的个数和名称和原来部分相同。

2、$newstags与$oldtags完全相同->不修改标签

3、$newstags与$oldtags完全不同 ->添加或减少标签,并且标签的个数和名称和原来完全不同 

对于2我们只要通过判断比较过滤即可,对于1、3通过判断比较后,再作删除或添加处理:

删除:要删除的标签名,在thinphp_tag已存在,当标签的count=1时,就把它删除掉,当count>1时,就减少1(count-1).

添加:要添加的标签名称,如果thinphp_tag表中已存在则count(count >1)在原来的基础上+1即count+1,如果不存在(count =0),则添加新标签,count+1.具体函数如下:

  1. public function updateTag($vo,$module) { 
  2.  
  3.                  
  4.  
  5.  $recordId= trim($vo['id']); 
  6.  
  7. if($vo['keywords']==''){//如果没有标签,则把原来的标签删除 
  8.  
  9.      $this->deltag($recordId);     
  10.  
  11.    }else
  12.  
  13.       $newtags = explode(' '$vo['keywords']);//获取更新的标签并转为数组(keywords是这里的标签字段,在thinkphp的拓展案例blog为tags,注意)                                  
  14.  
  15.    $condition['recordId'] = $recordId;//当有多个标签时,将查询多篇日记,而不是一篇 
  16.  
  17.  $tagged=M('Tagged'); 
  18.  
  19.   $tag=M('Tag');          
  20.  
  21.   $taggedlist$tagged->where($condition)->select(); 
  22.  
  23. if($taggedlist !==false){ 
  24.  
  25.                           
  26.  
  27. foreach ($taggedlist as $key => $value) { 
  28.  
  29.   $tagId=trim($value['tagId']); 
  30.  
  31.   $tagvo=$tag->where('id='.$tagId)->find(); 
  32.  
  33.   $oldtags[]=$tagvo['name'];//获取原来的标签 
  34.  
  35.   }    
  36.  
  37.    $result=count(array_diff(array_diff($newtags,$oldtags),array_diff($oldtags,$newtags)));     
  38.  
  39.    $result1=count(array_diff($newtags,$oldtags));//返回更新前后TAG的差值数 
  40.  
  41.   $result2=count(array_diff($oldtags,$newtags));//返回更新前后TAG的差值数 
  42.  
  43.                                                   
  44.  
  45.   if(($result1 !== $result2) || ($result !==0)){//2与原来的完全相同->过滤掉            
  46.  
  47.    $array_intersect=array_intersect($oldtags,$newtags);//取得交值 
  48.  
  49.    $oldtags_diff=array_diff($oldtags,$array_intersect);//原来的标签,被更新后已无用,需要删除的 
  50.  
  51.     $newtags_diff=array_diff($newtags,$array_intersect);//修改的标签,需要添加的 
  52.  
  53.                                                           
  54.  
  55. //删除或者count-1    
  56.  
  57.      if(count($oldtags_diff) !==0){  
  58.  
  59.                       
  60.  
  61.      foreach ($oldtags_diff as $name) { 
  62.  
  63.      $tagvo=$tag->where("module='$module' and name='$name'")->find(); 
  64.  
  65.      $count=intval($tagvo['count']);//获取标签的数量 
  66.  
  67.             
  68.  
  69. if($count==1){ 
  70.  
  71.     $tag->where('id='.$tagvo['id'])->delete(); 
  72.  
  73.     $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete(); 
  74.  
  75.  }elseif($count > 1){ 
  76.  
  77.                   
  78.  
  79.    $tag->where('id='.$tagvo['id'])->setDec('count',1);//标签数量减1 
  80.  
  81.    $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete();//删除tagged中相关数据  
  82.  }
  83.   }
  84. //添加更新的标签   
  85.  
  86. if(count($newtags_diff) !==0){ 
  87.  
  88.    foreach ($newtags_diff as $v) { 
  89.  
  90.        $v = trim($v);          
  91.  
  92.        if (!emptyempty($v)) { 
  93.  
  94.         // 记录已经存在的标签 
  95.  
  96.      $map['module'] = $module
  97.  
  98.         $map['name'] = $v
  99.  
  100.         $tagg = $tag->where($map)->find(); 
  101.  
  102.        if ($tagg) {//如果现在保存的标签与之前相同的标签累加 
  103.  
  104.        $tagId = $tagg['id']; 
  105.  
  106.           $tag->where('id=' . $tagg["id"])->setInc('count', 1);//count统计加1(这个函数有三个参数,默认加1) 
  107.  
  108.           } else {//如果是新添的标签,标签+1 
  109.  
  110.                    $t = array(); 
  111.  
  112.                    $t["name"] = $v
  113.  
  114.                    $t["count"] = 1; 
  115.  
  116.                    $t["module"] = $module
  117.  
  118.                    $result = $tag->add($t); 
  119.  
  120.                    $tagId = $result
  121.  
  122.             } 
  123.  
  124.       } 
  125.  
  126.                  //记录tag信息 
  127.  
  128.     $t = array(); 
  129.  
  130.       $t["module"] = $module
  131.  
  132.       $t["recordId"] = $recordId;//保存news的ID 
  133.  
  134.       $t["tagTime"] = time(); 
  135.  
  136.       $t["tagId"] = $tagId;//保存tag的ID 
  137.  
  138.       $tagged->add($t); 
  139.  
  140.      } 
  141.  
  142.     }  
  143.      } 
  144.      } 
  145.  
  146.      } 
  147. }  

使用方法:

  1. public  function update() { 
  2.        $Blog=D('Blog'); 
  3.        $vo=$Blog->create(); 
  4.          $this->updateTag($vo,'Blog');//更新前调用 
  5.              if (false === $vo) { 
  6.                         $this->error($Blog->getError()); 
  7.                      } 
  8.                    // 更新数据 
  9.                    $list = $Blog->save(); 
  10.                    if (false !== $list) { 
  11.                      //print_r($list); 
  12.                       
  13.                      $this->success('编辑成功!'); 
  14.                    } else { 
  15.                        //错误提示 
  16.                        $this->error('编辑失败!'); 
  17.                    } 
  18.                 } 

www.phpzy.comtrue/phpkj/5564.htmlTechArticle给TinkPHP添加一个更新的标签的方法 我们知道,thinkphp的拓展案例blog,只告诉我们怎样去添加标签tag,却没有删除和更新标签的方法,我在前面的《怎样彻底删除thinkphp案例blog的标签?》为拓展...

相关文章

相关频道:

PHP之友评论

今天推荐