欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ThinkPHP添加更新標(biāo)簽的方法

 更新時(shí)間:2014年12月05日 09:46:23   投稿:shichen2014  
這篇文章主要介紹了ThinkPHP添加更新標(biāo)簽的方法,在前文所述刪除blog標(biāo)簽的基礎(chǔ)上實(shí)現(xiàn)同步更新標(biāo)簽,是ThinkPHP非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了ThinkPHP添加更新標(biāo)簽的方法。分享給大家供大家參考。具體分析如下:

我們知道,thinkphp的拓展案例blog,只告訴我們?cè)鯓尤ヌ砑訕?biāo)簽tag,卻沒有刪除和更新標(biāo)簽的方法,我在前面的《徹底刪除thinkphp3.1案例blog標(biāo)簽的方法》為拓展案例blog寫了一個(gè)刪除標(biāo)簽的方法,接下來將寫一個(gè)標(biāo)簽的更新方法.

一般情況下,我們寫博客后,很少去改動(dòng)標(biāo)簽了,但是如果我們改動(dòng)標(biāo)簽如,刪除,添加,減少標(biāo)簽怎么辦呢?這無疑造成think_tag和think_tagged兩個(gè)表垃圾信息的積累,好了,言歸正轉(zhuǎn).

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

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

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

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

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

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

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

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

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

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

復(fù)制代碼 代碼如下:
public function updateTag($vo,$module) { 
 $recordId= trim($vo['id']); 
 
if($vo['keywords']==''){//如果沒有標(biāo)簽,則把原來的標(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'];//獲取原來的標(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與原來的完全相同->過濾掉            
 
   $array_intersect=array_intersect($oldtags,$newtags);//取得交值 
 
   $oldtags_diff=array_diff($oldtags,$array_intersect);//原來的標(biāo)簽,被更新后已無用,需要?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('編輯失敗!'); 
   } 
}

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論