Hi all,
all help is greatly appreciated - my head is spinning on this one!
Here's the situation...
I'm trying to create a photo 'tag' script - think keywords for photos.
What I've achieved so far:
The user after submitting their 'tags' - punctuation gets stripped and replaced with commas - and all white space is removed:
these,are,tags
I have then use
$tag = explode(',', $tags);
to create the array $tag - giving each word it's own usage eg:
$tag[0]
The next stage is to compare the values in this array to the existing "tag_name" column in my database:
if the word doesn't exist, store it and give it a unique id - if it already exists - don't do anything with it.
Eventually I will have a list of tag_names in a db. Every photo (in a separate table) will then have a 'tags' column - containing either the tag_id or tag_name (I haven't decided yet).
The reason I have got a separate 'tag' table is for ease of future 'search by tag' facilities.
Here is my code so far - please rip to bits and help compare the array $tag[] with the column tag_name...
....unless of course some one knows a slicker way to achieve my goal!
if (isset($_POST['submit']))
{
$tags = htmlentities($_POST['tags']); // Grab tags created by user
$separators = array(".", ",", ";", ":", "-", "_", "|"); //Create an array wit likely punctuation between tags
$tags = str_replace($separators, ',', $tags); // Replace any punctuation with a comma
$tags = str_replace(' ', '', $tags); // Remove any remaining white space
$tag = explode(',', $tags); // explode $tags, remove the commas and place in Array $tag
//You can now echo each tag individually
//echo $tag[0];
//echo $tag[1];
//echo $tag[2];
$tagname['tag_name'] = $value;
foreach ($tag as $tag=>$value)
{
$query = mysql_query("SELECT * FROM tags WHERE tag_name = '$value'");
}
if(mysql_num_rows($query) == 0)
{
//loop through the array
$number = count($tag);
for ($t=0; $t<$number; $t++)
{
$separatetag = $tag[$t];
if ($tag[$t] <> '')
{
mysql_query("INSERT INTO tags (tag_id, tag_name) VALUES ('', '$separatetag')");
}
}
}
}
?>
A mess I know - all help appreciated!!