溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ThinkPHP中怎么添加更新標(biāo)簽

發(fā)布時(shí)間:2021-07-09 16:59:52 來(lái)源:億速云 閱讀:150 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)ThinkPHP中怎么添加更新標(biāo)簽,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

在更新標(biāo)簽時(shí)我們來(lái)了解兩個(gè)參數(shù):

$oldtags:更新前,存在thinphp_tag表中標(biāo)簽

$newstags:更新時(shí),插入thinphp_tag之前,表單提交過(guò)來(lái)的標(biāo)簽

更新文章時(shí),標(biāo)簽可能會(huì)有以下幾種變化:

1、$newstags與$oldtags部分相同-> 添加或減少或修改標(biāo)簽,標(biāo)簽的個(gè)數(shù)和名稱(chēng)和原來(lái)部分相同。

2、$newstags與$oldtags完全相同->不修改標(biāo)簽

3、$newstags與$oldtags完全不同 ->添加或減少標(biāo)簽,并且標(biāo)簽的個(gè)數(shù)和名稱(chēng)和原來(lái)完全不同

對(duì)于2我們只要通過(guò)判斷比較過(guò)濾即可,對(duì)于1、3通過(guò)判斷比較后,再作刪除或添加處理:

刪除:要?jiǎng)h除的標(biāo)簽名,在thinphp_tag已存在,當(dāng)標(biāo)簽的count=1時(shí),就把它刪除掉,當(dāng)count>1時(shí),就減少1(count-1).

添加:要添加的標(biāo)簽名稱(chēng),如果thinphp_tag表中已存在則count(count >1)在原來(lái)的基礎(chǔ)上+1即count+1,如果不存在(count =0),則添加新標(biāo)簽,count+1.具體函數(shù)如下:

復(fù)制代碼 代碼如下:

public function updateTag($vo,$module) { 
 $recordId= trim($vo['id']); 
 
if($vo['keywords']==''){//如果沒(méi)有標(biāo)簽,則把原來(lái)的標(biāo)簽刪除 
     $this->deltag($recordId);     
   }else{ 
      $newtags = explode(' ', $vo['keywords']);//獲取更新的標(biāo)簽并轉(zhuǎn)為數(shù)組(keywords是這里的標(biāo)簽字段,在thinkphp的拓展案例blog為tags,注意)
 
   $condition['recordId'] = $recordId;//當(dāng)有多個(gè)標(biāo)簽時(shí),將查詢多篇日記,而不是一篇 
 
 $tagged=M('Tagged'); 
 
  $tag=M('Tag');          
 
  $taggedlist= $tagged->where($condition)->select(); 
 
if($taggedlist !==false){ 
 
foreach ($taggedlist as $key => $value) { 
 
  $tagId=trim($value['tagId']); 
 
  $tagvo=$tag->where('id='.$tagId)->find(); 
 
  $oldtags[]=$tagvo['name'];//獲取原來(lái)的標(biāo)簽 
 
  }    
 
   $result=count(array_diff(array_diff($newtags,$oldtags),array_diff($oldtags,$newtags)));     
 
   $result1=count(array_diff($newtags,$oldtags));//返回更新前后TAG的差值數(shù) 
 
  $result2=count(array_diff($oldtags,$newtags));//返回更新前后TAG的差值數(shù)
 
  if(($result1 !== $result2) || ($result !==0)){//2與原來(lái)的完全相同->過(guò)濾掉            
 
   $array_intersect=array_intersect($oldtags,$newtags);//取得交值 
 
   $oldtags_diff=array_diff($oldtags,$array_intersect);//原來(lái)的標(biāo)簽,被更新后已無(wú)用,需要?jiǎng)h除的 
 
    $newtags_diff=array_diff($newtags,$array_intersect);//修改的標(biāo)簽,需要添加的 
 
//刪除或者count-1    
 
     if(count($oldtags_diff) !==0){  
 
     foreach ($oldtags_diff as $name) { 
 
     $tagvo=$tag->where("module='$module' and name='$name'")->find(); 
 
     $count=intval($tagvo['count']);//獲取標(biāo)簽的數(shù)量 
 
if($count==1){ 
 
    $tag->where('id='.$tagvo['id'])->delete(); 
 
    $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete(); 
 
 }elseif($count > 1){ 
   $tag->where('id='.$tagvo['id'])->setDec('count',1);//標(biāo)簽數(shù)量減1 
 
   $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete();//刪除tagged中相關(guān)數(shù)據(jù)  
 }
  }

//添加更新的標(biāo)簽   
 
if(count($newtags_diff) !==0){ 
 
   foreach ($newtags_diff as $v) { 
 
       $v = trim($v);          
 
       if (!emptyempty($v)) { 
 
        // 記錄已經(jīng)存在的標(biāo)簽 
 
     $map['module'] = $module; 
 
        $map['name'] = $v; 
 
        $tagg = $tag->where($map)->find(); 
 
       if ($tagg) {//如果現(xiàn)在保存的標(biāo)簽與之前相同的標(biāo)簽累加 
 
       $tagId = $tagg['id']; 
 
          $tag->where('id=' . $tagg["id"])->setInc('count', 1);//count統(tǒng)計(jì)加1(這個(gè)函數(shù)有三個(gè)參數(shù),默認(rèn)加1) 
 
          } else {//如果是新添的標(biāo)簽,標(biāo)簽+1 
 
                   $t = array(); 
 
                   $t["name"] = $v; 
 
                   $t["count"] = 1; 
 
                   $t["module"] = $module; 
 
                   $result = $tag->add($t); 
 
                   $tagId = $result; 
 
            } 
      } 
                 //記錄tag信息 
    $t = array(); 
 
      $t["module"] = $module; 
 
      $t["recordId"] = $recordId;//保存news的ID 
 
      $t["tagTime"] = time(); 
 
      $t["tagId"] = $tagId;//保存tag的ID 
 
      $tagged->add($t); 
 
     } 
 
    }  
     } 
     } 
     } 
}


使用方法:

復(fù)制代碼 代碼如下:

public  function update() { 
$Blog=D('Blog'); 
$vo=$Blog->create(); 
$this->updateTag($vo,'Blog');//更新前調(diào)用 
if (false === $vo) { 
 $this->error($Blog->getError()); 
     } 
   // 更新數(shù)據(jù) 
   $list = $Blog->save(); 
   if (false !== $list) { 
     //print_r($list); 
      
     $this->success('編輯成功!'); 
   } else { 
       //錯(cuò)誤提示 
       $this->error('編輯失敗!'); 
   } 
}

關(guān)于ThinkPHP中怎么添加更新標(biāo)簽就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI