Home Backend Development PHP Tutorial ThinkPHP添加更新标签的方法_php实例

ThinkPHP添加更新标签的方法_php实例

Jun 07, 2016 pm 05:14 PM
thinkphp renew Label Add to

本文实例讲述了ThinkPHP添加更新标签的方法。分享给大家供大家参考。具体分析如下:

我们知道,thinkphp的拓展案例blog,只告诉我们怎样去添加标签tag,却没有删除和更新标签的方法,我在前面的《彻底删除thinkphp3.1案例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.具体函数如下:

复制代码 代码如下:
public function updateTag($vo,$module) { 
 $recordId= trim($vo['id']); 
 
if($vo['keywords']==''){//如果没有标签,则把原来的标签删除 
     $this->deltag($recordId);     
   }else{ 
      $newtags = explode(' ', $vo['keywords']);//获取更新的标签并转为数组(keywords是这里的标签字段,在thinkphp的拓展案例blog为tags,注意)
 
   $condition['recordId'] = $recordId;//当有多个标签时,将查询多篇日记,而不是一篇 
 
 $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'];//获取原来的标签 
 
  }    
 
   $result=count(array_diff(array_diff($newtags,$oldtags),array_diff($oldtags,$newtags)));     
 
   $result1=count(array_diff($newtags,$oldtags));//返回更新前后TAG的差值数 
 
  $result2=count(array_diff($oldtags,$newtags));//返回更新前后TAG的差值数
 
  if(($result1 !== $result2) || ($result !==0)){//2与原来的完全相同->过滤掉            
 
   $array_intersect=array_intersect($oldtags,$newtags);//取得交值 
 
   $oldtags_diff=array_diff($oldtags,$array_intersect);//原来的标签,被更新后已无用,需要删除的 
 
    $newtags_diff=array_diff($newtags,$array_intersect);//修改的标签,需要添加的 
 
//删除或者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']);//获取标签的数量 
 
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);//标签数量减1 
 
   $tagged->where('tagId='.$tagvo['id'].' and recordId='.$recordId)->delete();//删除tagged中相关数据  
 }
  }

//添加更新的标签   
 
if(count($newtags_diff) !==0){ 
 
   foreach ($newtags_diff as $v) { 
 
       $v = trim($v);          
 
       if (!emptyempty($v)) { 
 
        // 记录已经存在的标签 
 
     $map['module'] = $module; 
 
        $map['name'] = $v; 
 
        $tagg = $tag->where($map)->find(); 
 
       if ($tagg) {//如果现在保存的标签与之前相同的标签累加 
 
       $tagId = $tagg['id']; 
 
          $tag->where('id=' . $tagg["id"])->setInc('count', 1);//count统计加1(这个函数有三个参数,默认加1) 
 
          } else {//如果是新添的标签,标签+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); 
 
     } 
 
    }  
     } 
     } 
     } 
}

使用方法:
复制代码 代码如下:
public  function update() { 
$Blog=D('Blog'); 
$vo=$Blog->create(); 
$this->updateTag($vo,'Blog');//更新前调用 
if (false === $vo) { 
 $this->error($Blog->getError()); 
     } 
   // 更新数据 
   $list = $Blog->save(); 
   if (false !== $list) { 
     //print_r($list); 
      
     $this->success('编辑成功!'); 
   } else { 
       //错误提示 
       $this->error('编辑失败!'); 
   } 
}

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fix Blizzard Battle.net update stuck at 45%? How to fix Blizzard Battle.net update stuck at 45%? Mar 16, 2024 pm 06:52 PM

Blizzard Battle.net update keeps stuck at 45%, how to solve it? Recently, many people have been stuck at the 45% progress bar when updating software. They will still get stuck after restarting multiple times. So how to solve this situation? We can reinstall the client, switch regions, and delete files. To deal with it, this software tutorial will share the operation steps, hoping to help more people. Blizzard Battle.net update keeps stuck at 45%, how to solve it? 1. Client 1. First, you need to confirm that your client is the official version downloaded from the official website. 2. If not, users can enter the Asian server website to download. 3. After entering, click Download in the upper right corner. Note: Be sure not to select Simplified Chinese when installing.

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Windows cannot access the specified device, path, or file Windows cannot access the specified device, path, or file Jun 18, 2024 pm 04:49 PM

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

How to add a new script in Tampermonkey-How to delete a script in Tampermonkey How to add a new script in Tampermonkey-How to delete a script in Tampermonkey Mar 18, 2024 pm 12:10 PM

Tampermonkey Chrome extension is a user script management plug-in that improves user efficiency and browsing experience through scripts. So how does Tampermonkey add new scripts? How to delete the script? Let the editor give you the answer below! How to add a new script to Tampermonkey: 1. Take GreasyFork as an example. Open the GreasyFork web page and enter the script you want to follow. The editor here chooses one-click offline download. 2. Select a script. , after entering the script page, you can see the button to install this script. 3. Click to install this script to come to the installation interface. Just click here to install. 4. We can see the installed one-click in the installation script.

See all articles