thinkphp Add Update label method _php instance

Source: Internet
Author: User

The example in this article describes how thinkphp adds update labels. Share to everyone for your reference. The specific analysis is as follows:

We know, thinkphp expansion Case blog, just tell us how to add tag tag, but did not delete and update the label method, I in front of the "completely delete the thinkphp3.1 Case Blog tag method" for the expansion of the case blog wrote a way to delete the label, Next you will write an update method for the label.

Under normal circumstances, we write a blog, rarely to change the label, but if we change the label such as, delete, add, reduce the label how to do? This will undoubtedly result in the accumulation of two of think_tag and think_tagged garbage information, well, the word is turning.

We'll look at two parameters when we update the labels:

$oldtags: Before updating, there are labels in the Thinphp_tag table

$newstags: The label submitted by the form before inserting the thinphp_tag when updating

When you update an article, there are several possible changes to the label:

1, $newstags and $oldtags part of the same-> add or reduce or modify the label, the number and name 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 labels, and the number and name of labels and original completely different

For 2, we can only pass the judgement comparison filter, for 1, 3 after the comparison, then delete or add processing:

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

Add: The name of the label to add, if the Thinphp_tag table already exists, count (Count >1) is on the original +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 Code code as follows:
Public Function Updatetag ($VO, $module) {
$recordId = Trim ($vo [' id ']);

if ($vo [' keywords ']== ') {///If there is no label, delete the original label
$this->deltag ($recordId);
}else{
$newtags = Explode (', $vo [' keywords '])//Get updated label and turn to Array (keywords is the label field here, in the thinkphp expansion case Blog for tags, note)

$condition [' recordid '] = $recordId;//When there are multiple labels, you will query for more than one journal, not a

$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 number of the tag difference before and after the update

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

if ($result 1!== $result 2) | | ($result!==0)) {//2 with the original exactly the same-> filtered out

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

$oldtags _diff=array_diff ($oldtags, $array _intersect);//original label, after being updated, is useless and needs to be deleted.

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

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);//label quantity 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 a label that already exists

$map [' module '] = $module;

$map [' name '] = $v;

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

if ($tagg) {//If the label you are saving is now cumulative with 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, the 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 News ID

$t ["tagtime"] = time ();

$t ["tagid"] = $tagId;//save tag ID

$tagged->add ($t);

}

}
}
}
}
}

How to use:
Copy Code code 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 (' Edit successful! ');
} else {
Error tips
$this->error (' Edit failed! ');
}
}

I hope this article will help you with the PHP program design based on 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.