Forum Moderators: coopster
What I have is a database with the columns id, name, email, tags, etc. In the "tags" column (which may or may not be empty), the tags are entered as comma separated values like so: tag1, tag2, tag3,
I need to be able to select all the tags from the database (WHERE tags != ""), and then do an explode of some sort to separate all the tags using the commas. Also, if there are two or more identical tags in different rows, I need it to only show each unique tag once.
Any help would be greatly appreciated. Thanks guys.
e.g. article_tag(article_id, tag_id)
You can then just use inner joins to select the tag, the number of times it's id occurs and a particular article, this should help you with the unique problem as the tag is only ever stored once regardless of how many things it is associated with.
$query = "SELECT tags FROM table";
$result = mysql_query ($query);
$tags = array();
while($row = mysql_fetch_assoc($result)) {
if ($row['tags'] != '') {
array_push($tags, $row['tags']);
}
}
$foo = implode(",", $tags);
$tags = explode(',', $foo);
$tagData = '';
foreach ($tags AS $tags)
{
$tagData .= "<a href=\"/search.php?tag=$tags\">$tags</a>, ";
}
echo substr_replace($tagData, '', -2);
Basically it goes through and adds new tags into the "$tags" array where the field is not null. Then, I implode the array into a single variable, and then explode the variable using the comma as the delimiter.
The substr_replace() is to remove trailing commas from the final results.
Here is model you may look at
sorry, I won't have time to support it
but you will get the idea
// enter tags in db
// hereI require at least two tags per entry$tags=trim($_POST["tags"]); //echo"tttt $tags<P>";
if (empty($tags))
{
echo"<b>The tag field is empty<br>Please etc.....";
exit();
}////// check if at least 2 words are in array
$tg=explode(",", $tags);
$num_tag=count($tg);
if(($num_tag <2))
{
echo"You entered $num_tag tag number, you need entering 2 words minimum
etc.....";
exit();
}////// end check array word num
$str=$tags;
$str=strlen($tags); //echo"strlen $str<P>";
if($str>100)
{
echo"<b>The submited total tag combo is longer than 100 characters and space<br>Please etc......";
exit();
}if (!preg_match("/^[a-z-0-9\,\ ]+$/",$str) )
{
echo"<b>The submited tag should only contain alphanumerical lower case characters and commas!<br>
Please <etc...";
exit();
}// select tags from DB ////////////////////////////////////
SELECT
tags
FROM
#*$!#*$!xx
WHERE
zzzzz='$zzzzzz'
");
if (!$result)
{
echo "DB Error, could not list sections\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}while ($row=$db->fetch_array($result_tags) )
{
$tags=$row['tags'];$tags=unserialize($tags); // print_r($tags);
$tags=array($tags);
$tags=array_merge(array_unique($tags)); //foreach($tags as $key=>$tags)
{
echo"<a href='your main page.php?rrrrrrrt=$rrrrrr&tags=$tags' target='iframe'>Click the tags to read the tags related whatever => $tags</a><br> <br>";
}
}
echo"<iframe name='iframe' width='980' height='200' scrolling='yes'>";
Basically the rows in the database could have the following tags (duplicates are intentional):
row1: tag1, tag2, tag2, tag3
row2: tag4
row3: tag5, tag6, tag3
I use the following code to loop through the array and add all the tags in the database to the $tags array.
$tags = array();
while($row = mysql_fetch_assoc($result)) {
if ($row['tags'] != '') {
array_push($tags, $row['tags']);
}
}
I then implode the $tags array to get all the values into one variable like so:
$foo = implode(",", $tags);
Then I explode the consolidated array to create a new array with the tags separated by commas.
$tags = explode(',', $foo);
Then, I loop through my new $tags array and echo out the individual tags like so:
$tagData = '';
foreach ($tags AS $tags)
{
$tagData .= "<a href=\"/search.php?tag=$tags\">$tags</a>, ";
}
echo substr_replace($tagData, '', -2);
The substr_replace() part is just to get rid of trailing commas, but it may be what's causing the problem, I don't know.
What I need to do, is find a way to remove duplicate entries from the $tags array before I echo the tags out. I've tried $tags = array_unique($tags), to try to consolidate it, and it worked but only for certain tags. Other tags would still continue to display even if they were duplicates.
Any help you could offer would be greatly appreciated. Thanks.
It does cut out some duplicate tags, like if I have two rows in the database that only have one tag each, and each tag is identical, array_unique() will filter out the second identical tags. But that's not always the case, and if I have two rows with tags like so:
row1: tag1, tag2, tag3
row2: tag4, tag1, tag5
It often won't detect and filer out the two identical tags. It also won't detect and filter out the tags if they are both originally in the same row like this:
row1: tag1, tag1, tag2
I'm not sure what the cause of that could be. I'm using the array_unique() function after I've already exploded the data into an array where each tag is an independent entry in the array.
"...so that you can compare them" - Who's comparing anything? I'm not comparing. Are you comparing?
Anyway, I found my own solution after a few hours and half a pack of cigs (I'm new to PHP. I know the solution was probably simple to most of you, but lay off ;-) ).
For the next googler who comes after me, here's what I did after the implode and explode:
foreach ($tags as $tag)
{
$tag = ltrim($tag);
array_push($tags_final, $tag);
}
$tags = array_unique($tags_final);
Hope that helps. It might not be the most logical or efficient solution, but it works (Maybe this could have been done in the while loop, but I couldn't figure out how).
And seriously people, if you're going to respond to a message, make sure your response makes sense and assume the person who will be reading it doesn't know what you know.
BTW, kudos to ZeroDesigns. During my search I found a lot of long, drawn out, convoluted solutions to this problem. His was simple and straight-forward. Thanks buddy.
This forum is all about helping each other and there is a good atmosphere here. I guess I might just go back to being a lurker, at least that way no one has a pop at you for not leaving replies