tags:

views:

347

answers:

2

Hi,

I am implementing a tag cloud system based on this recommendation. (However, I am not using foreign keys)

I am allowing up to 25 tags. My question is, how can I handle editing on the items? I have this item adding/editing page:

Title: Description: Tags: (example data) computer, book, web, design

If someone edits an item details, do I need to delete all the tags from Item2Tag table first, then add the new elements? For instance, someone changed the data to this:

Tags: (example data) computer, book, web, newspaper

Do I need to delete all the tags from Item2Tag table, and then add these elements? It seems inefficient, but I could find a more efficient way.

The other problem is with this way is, if someone edits description but does not change the tags box, I still need to delete all the elements from Item2Tag table and add the same element.

I am not an experienced PHP coder, so could you suggest a better way to handle this? (pure PHP/MySQL solution is preferable)

Thanks in advance,

A: 

You should have a primary key on the Item2Tag table so just try and insert the links to the new tags - this will however error out if an item2tag link already exists. Depending on the DBMS you are using there are different ways of handling this. MySQL provides the following syntax:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

Which is equivalent to:

UPDATE table SET c=c+1 WHERE a=1;

If the record already exists.

For your second problem, well you shouldn't need to alter the tags if the item_id of the item is not changed. The Item2Tag table links an item to a tag, but if item_id remains constant there is no need to alter the tags.

mdm
Thanks for the answer, however it does not fully answer my question. If I use this query, there will be old tags in the database. If you look at my example data in the question, I also need to delete the "design" tag from it. For the second question, I can't verify whether the tags box changed or not. How can I verify it? (It seems the only way is to request all the tags again before updating the row and compare them, which seems inefficient. I believe there should be a better solution for the all question.)
deniz
Ah ok I think I got a bit confused there!You could tell whether or not the tags had been changed by keeping a hash of the tags against the item, e.g. md5("computer" + "book" + "web" + "design"), and then only updating the tags if the stored hash and the hash of the new set of tags are different. This stops you from having to get all the tags before then deleting them and updating again.Jimmy Shelter's answer below using a delta is another good way of doing this. You could pass the existing tags or hash via get or post to save you having to look them up when processing the update.
mdm
A: 

In your system you already know the current tags, so what you can do is make a delta.

To use your own example, these are the starting tags: computer, book, web, design

And someone changes this too: computer, book, web, newspaper

// Quick & dirty example (pseudo)code
$current_tags = array('computer', 'book', 'web', 'design');
$new_tags = array('computer', 'book', 'web', 'newspaper');

foreach ($new_tags as $key => $new_tag) {
  if (!in_array($new_tag, $current_tags) {
    $tags_to_add[] = $new_tag;
  } else {
    // Delete item from $current_tags
  }
}
// Now delete the items left in $current_tags
// And then add the items in $tags_to_add
Jimmy Shelter
Seems a long way around to re-implement PHP's array_diff() :)
TML
e.g.$current_tags = array('computer', 'book', 'web', 'design');$new_tags = array('computer', 'book', 'web', 'newspaper');$added = array_diff($new_tags, $current_tags);$removed = array_diff($current_tags, $new_tags);The subsequent SQL should be pretty simple.
TML
Doh! You're completely right.
Jimmy Shelter