Removing unused tags from DB-> Compare Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Removing unused tags from DB-> Compare Arrays

Post by JAB Creations »

I'm trying to determine the best way to remove tags that are no longer used in by a blog post from my database table.

Essentially tags are received by the server in the following format when a form is used for editing an existing post (note I've simply converted $_POST to actual values to keep it simple)...

Code: Select all

$thread_tags = 'application/xhtml+xml, XML, XHTML, CSS';
$thread_tags_old = 'application/xhtml+xml, XML, XHTML, CSS, JavaScript';
If you take a moment to compare the values in both variables it's clear that in this procedure that we'll want to delete the MySQL row that contains the value JavaScript (in the relational table).

There are most likely numerous ways to approach this with the most confusing and least efficient most likely being my initial attempts. I've been thinking of converting these variables in to arrays (is there a nice PHP function to convert a nicely formatted variable or will we have to explode first?) and then using a loop (obviously) to compare the arrays. Then during the loop use something along the lines of the in_array function to determine if it's !in_array and then to queue it in to a MySQL query that I'll delete all of the removed tags from the database after the loop.

That is at least how I am thinking of approaching this task so I'm open to suggestions please!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Removing unused tags from DB-> Compare Arrays

Post by Eran »

is there a nice PHP function to convert a nicely formatted variable or will we have to explode first?
What's wrong with explode? it does exactly what you are asking for.

Code: Select all

 
$thread_tags = 'application/xhtml+xml, XML, XHTML, CSS';
$thread_tags_old = 'application/xhtml+xml, XML, XHTML, CSS, JavaScript';
 
$tags_array = explode(', ',$thread_tags);
$old_tags_array = explode(', ',$thread_tags_old);
 
$remove_tags = array_diff($old_tags_array,$tags_array);
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Removing unused tags from DB-> Compare Arrays

Post by JAB Creations »

That's almost more artwork then code, thanks! :mrgreen:

I can simply throw in a foreach loop and execute a MySQL query to remove each entry...or maybe a single MySQL entry...not exactly sure.

Code: Select all

<?php
$thread_tags = 'application/xhtml+xml, XML, XHTML, CSS';
$thread_tags_old = 'application/xhtml+xml, XML, XHTML, CSS, JavaScript, PHP, MySQL';
 
$tags_array = explode(', ',$thread_tags);
$old_tags_array = explode(', ',$thread_tags_old);
 
$remove_tags = array_diff($old_tags_array,$tags_array);
print_r($remove_tags);
 
foreach ($remove_tags as $value)
{
 echo '<div>'.$value.'</div>';
}
?>
Thoughts please?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Removing unused tags from DB-> Compare Arrays

Post by Eran »

One query:

Code: Select all

 
$query = "DELETE FROM `table_name_here` WHERE `post_id_column_here`=`post_id_here` AND (`tag_column` = '" . implode(" OR `tag_column`='",$remove_tags) . "')"; 
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Removing unused tags from DB-> Compare Arrays

Post by JAB Creations »

Why implode instead of explode if they're already in an array?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Removing unused tags from DB-> Compare Arrays

Post by JAB Creations »

I still can't get my head wrapped around how one can implode an array...since to me it's something you implode *to* and explode *from*.

However I was finally able to see the output when I stuck the implode inside a print_r function. Thanks, I really did not expect to receive such a short though powerful piece of code!

I'm very interested in why an implode works here because I've been thinking in reverse-ploding.
Post Reply