Page 1 of 1

Removing unused tags from DB-> Compare Arrays

Posted: Fri Nov 14, 2008 5:47 am
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!

Re: Removing unused tags from DB-> Compare Arrays

Posted: Fri Nov 14, 2008 8:28 am
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);
 

Re: Removing unused tags from DB-> Compare Arrays

Posted: Mon Nov 17, 2008 6:27 am
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?

Re: Removing unused tags from DB-> Compare Arrays

Posted: Mon Nov 17, 2008 6:52 am
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) . "')"; 
 

Re: Removing unused tags from DB-> Compare Arrays

Posted: Fri Nov 21, 2008 5:01 am
by JAB Creations
Why implode instead of explode if they're already in an array?

Re: Removing unused tags from DB-> Compare Arrays

Posted: Fri Nov 21, 2008 5:10 am
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.