ThinkPHP Method for adding and updating tags _ php instance

Source: Internet
Author: User
This article mainly introduces how to add and update tags for ThinkPHP. It is a very practical technique for ThinkPHP to synchronously update tags Based on the deletion of blog tags described above, for more information about how to add and update tags in ThinkPHP, see the following example. Share it with you for your reference. The specific analysis is as follows:

We know that thinkphp's extended case blog only tells us how to add tags, but there is no way to delete or update tags, in my previous "How to permanently delete the thinkphp3.1 case blog tag", I wrote a method to delete the tag for the extended case blog. Next I will write a method to update the tag.

Generally, after we write a blog, we seldom modify tags. But what if we change tags such as, delete, add, and reduce tags? This will undoubtedly result in the accumulation of junk information in the think_tag and think_tagged tables.

When updating tags, we will understand two parameters:

$ Oldtags: The tag in the thinphp_tag table exists before the update.

$ Newstags: The tag submitted by the form before thinphp_tag is inserted during update.

When updating an article, the tag may have the following changes:

1. $ newstags is the same as $ oldtags-> add or remove or modify tags. The number and name of tags are the same as those of the original one.

2. $ newstags and $ oldtags are exactly the same-> do not modify tags

3. $ newstags and $ oldtags are completely different-> add or remove tags, and the number and name of tags are completely different from the original one.

For 2, we only need to filter by comparison. For 1 and 3, after comparison, we will delete or add the processing:

Delete: name of the tag to be deleted. The tag already exists in thinphp_tag. When count is 1, delete the tag. When count is> 1, reduce 1 (count-1 ).

Add: name of the tag to be added. If the thinphp_tag table already exists, count (count> 1) is based on the original value + 1, that is, count + 1. If it does not exist (count = 0 ), add a new tag, count + 1. the specific functions are as follows:

The Code is as follows:

Public function updateTag ($ vo, $ module ){
$ RecordId = trim ($ vo ['id']);

If ($ vo ['keyword'] = '') {// if no tag exists, delete the original tag.
$ This-> deltag ($ recordId );
} Else {
$ Newtags = explode ('', $ vo ['keyword']); // obtain the updated tag and convert it to an array (keywords is the tag field here, the extended case blog in thinkphp is tags. note)

$ Condition ['recordid'] = $ recordId; // when there are multiple tags, multiple Diaries will be queried, instead of one

$ Tagged = M ('taged ');

$ 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']; // obtain the original tag

}

$ Result = count (array_diff ($ newtags, $ oldtags), array_diff ($ oldtags, $ newtags )));

$ Result1 = count (array_diff ($ newtags, $ oldtags); // returns the difference value of the TAG before and after update.

$ Result2 = count (array_diff ($ oldtags, $ newtags); // returns the difference value of the TAG before and after update.

If ($ result1! ==$ Result2) | ($ result! = 0) {// 2 is exactly the same as the original one-> filter out

$ Array_intersect = array_intersect ($ oldtags, $ newtags); // obtain the intersection Value

$ Oldtags_diff = array_diff ($ oldtags, $ array_intersect); // The original tag, which is useless after being updated and needs to be deleted

$ Newtags_diff = array_diff ($ newtags, $ array_intersect); // modify the tag to be added

// Delete or 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']); // obtain the number of tags

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); // The number of tags minus 1

$ Tagged-> where ('tagid = '. $ tagvo ['id']. and recordId = '. $ recordId)-> delete (); // delete related data in tagged
}
}
}
// Add updated tags

If (count ($ newtags_diff )! = 0 ){

Foreach ($ newtags_diff as $ v ){

$ V = trim ($ v );

If (! Emptyempty ($ v )){

// Record existing tags

$ Map ['module'] = $ module;

$ Map ['name'] = $ v;

$ Tagg = $ tag-> where ($ map)-> find ();

If ($ tagg) {// if the currently saved tags are accumulated with the same existing tags

$ TagId = $ tagg ['id'];

$ Tag-> where ('Id = '. $ tagg ["id"])-> setInc ('Count', 1); // count statistics plus 1 (this function has three parameters. The default value is 1)

} Else {// If the tag is newly added, the tag is + 1

$ T = array ();

$ T ["name"] = $ v;

$ T ["count"] = 1;

$ T ["module"] = $ module;

$ Result = $ tag-> add ($ t );

$ TagId = $ result;

}
}
// Record tag Information
$ T = array ();

$ T ["module"] = $ module;

$ T ["recordId"] = $ recordId; // Save the news ID

$ T ["tagTime"] = time ();

$ T ["tagId"] = $ tagId; // ID of the tag to be saved

$ Tagged-> add ($ t );

}

}
}
}
}
}


Usage:

The Code is as follows:

Public function update (){
$ Blog = D ('blog ');
$ Vo = $ Blog-> create ();
$ This-> updateTag ($ vo, 'blog '); // called before update
If (false ===$ vo ){
$ This-> error ($ Blog-> getError ());
}
// Update data
$ List = $ Blog-> save ();
If (false! ==$ List ){
// Print_r ($ list );

$ This-> success ('edited successfully! ');
} Else {
// Error message
$ This-> error ('edit failed! ');
}
}

I hope this article will help you design PHP programs based on the ThinkPHP framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.