thinkphp Add Update label method _php instance

Source: Internet
Author: User
The example in this article describes how to add update tags to thinkphp. Share to everyone for your reference. The specific analysis is as follows:

We know, thinkphp's expansion case blog, just tell us how to add tag tag, but did not delete and update the method of the label, I in the previous "completely delete thinkphp3.1 Case Blog tag method" For the expansion case blog wrote a way to delete the label, The next step is to write a label update method.

In general, after we write a blog, we seldom go to change the label, but if we change the label such as, delete, add, reduce the label how to do? This undoubtedly caused Think_tag and think_tagged two table garbage information accumulation, good, the words return to positive turn.

When updating the label, let's look at two parameters:

$oldtags: There is a label in the Thinphp_tag table before the update

$newstags: The label submitted by the form is updated when Thinphp_tag is inserted

When you update an article, the label may change as follows:

1, $newstags and $oldtags part of the same, add or reduce or modify the label, the number and name of the label and the original part of the same.

2, $newstags and $oldtags exactly the same, do not modify the label

3, $newstags and $oldtags completely different, add or reduce the label, and the number and name of the label and the original completely different

For 2 We just by the judge comparison filter can, for 1, 3 by judging comparison, then delete or add processing:

Delete: To delete the name of the tag, in the Thinphp_tag already exist, when the count=1 of the label, delete it, when count>1, the reduction of 1 (count-1).

Add: To add a label name, if the Thinphp_tag table already exists then count (Count >1) on the original basis +1 that is count+1, if it does not exist (count =0), add a new label, count+1. The specific function is as follows:
Copy CodeThe code is as follows: Public function Updatetag ($VO, $module) {
$recordId = Trim ($vo [' id ']);

if ($vo [' keywords ']== ') {//If there is no label, remove the original label
$this->deltag ($recordId);
}else{
$newtags = Explode (', $vo [' keywords ']);//Get the updated label and convert it to an array (keywords is the label field here, in the thinkphp of the expansion case blog for tags, note)

$condition [' recordId '] = $recordId;//When multiple labels are available, multiple journals will be queried instead of one

$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 '];//get the original label

}

$result =count (Array_diff (Array_diff ($newtags, $oldtags), Array_diff ($oldtags, $newtags));

$result 1=count (Array_diff ($newtags, $oldtags));//Returns the value of the tag before and after the update

$result 2=count (Array_diff ($oldtags, $newtags));//Returns the value of the tag before and after the update

if (($result 1!== $result 2) | | ($result!==0)) {//2 filter out exactly the same as the original

$array _intersect=array_intersect ($oldtags, $newtags);//Get the intersection value

$oldtags _diff=array_diff ($oldtags, $array _intersect);//The original label, was updated after the useless, need to delete the

$newtags _diff=array_diff ($newtags, $array _intersect);//Modify the label, you need to add the

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 ']);//Get the number of labels

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

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

if (count ($newtags _diff)!==0) {

foreach ($newtags _diff as $v) {

$v = Trim ($v);

if (!emptyempty ($v)) {

Record labels that already exist

$map [' module '] = $module;

$map [' name '] = $v;

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

if ($tagg) {//If the label now saved is added to the same label as before

$tagId = $tagg [' id '];

$tag->where (' id= '. $tagg ["id"])->setinc (' Count ', 1);//count statistics plus 1 (this function has three parameters, default plus 1)

} else {//If it is a newly added label, label +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;//save tag ID

$tagged->add ($t);

}

}
}
}
}
}
How to use:
Copy CodeThe code is as follows: Public Function update () {
$Blog =d (' Blog ');
$vo = $Blog->create ();
$this->updatetag ($vo, ' Blog ');//Call before update
if (false = = = $vo) {
$this->error ($Blog->geterror ());
}
Update data
$list = $Blog->save ();
if (false!== $list) {
Print_r ($list);

$this->success (' editorial success! ');
} else {
Error hints
$this->error (' Edit failed! ');
}
}

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

  • Related Article

    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.