开发者

Updating tags (Toxi solution) Problem

so i have six catagories that entries in my database are tagged against. When the original entries are inserted, they are selected from a checkbox form, POSTED as an array and inserted as follows:

foreach ($cat as $item) 
{
$sql = 
"INSERT INTO tag_relational (job_id, tag_id) VALUES ('$job_id', '$item')";
$result=mysql_query($sql);
}

I now have an 'edit' section where people can edit their listings, but i'm a bit stuck as how to update the tags. I have tried this:

foreach ($cat as $item) 
{
$sql = 
"UPDATE tag_relational SET tag_id ='$item' where job_id = $job_id";
$result=mysql_query($sql);
}

But all it seems to do is set ea开发者_开发知识库ch existing entry to the same tag_id.

For example, say the original post featured four different tags (which are all pre-set by the way, they can only choose from a certain list), the above would set each of original job_id entries against the LAST, NEW, tag_id on the edit form, rather than any new tags chosen. Does that make sense?

Can anyone help edit my update query?


From what I understand, I think you need to manipulate your data first...

  1. Create arrays
  2. Check to see if the user marked it ($newTags)
  3. If they did mark it, check if it's already in the existing array. If not, INSERT it
  4. If they did NOT mark it, check if it's in the existing array. If it is, then delete it

Example:

$availableTags=array(1,2,3,4,5);
$existingTags=array(2,3,5);
$newTags=array(1,2);

foreach ($availableTags as $value)
{
   if(in_array($value,$newTags)){ //This SHOULD be in your table
      if(in_array($value,$existingTags)){/* Already in tag_relational, ignore it*/}
      else {/* INSERT it into tag_relational */}
   }
   else { //This should NOT be in your table, check if it already existed
     if(in_array($value,$existingTags)){/* DELETE it from tag_relational*/}
   }
}

Hopefully this makes sense. It should be possible to write a more concise version of this code!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜