Distinct on a delimited query

September 13, 2009 at 17:21:16
Specs: Windows XP
I am trying to select a delimited string (eg. fun, stuff) from a MySQL database and then check to see there are no duplicates.

This is what is in my database

test, stuff 
test
stuff

This is what I have so far:

<?php
$sql="SELECT DISTINCT tags FROM stories";
$res=mysql_query($sql) or die($sql." - ".mysql_error());
while($row=mysql_fetch_array($res))
{
$string = $row[0];
$array = explode(',', $string); 
foreach ($array as $value) 
{
echo "<a href=\"?mode=3&tag=".trim($value)."\">".trim($value) . "</a>
";
}
}

the only problem is that it does not stop duplicates
it outputs

test
stuff
test
stuff


See More: Distinct on a delimited query

Report •


#1
September 17, 2009 at 15:26:47
I figured it out
<?php
$sql="SELECT DISTINCT tags FROM stories";
$res=mysql_query($sql) or die($sql." - ".mysql_error());
while($row=mysql_fetch_array($res))
{
   $string = $row[0];
   $array = explode(',', $string);
   foreach ($array as $value)
   {
      $bigarray[] = trim($value);
   }
}
$bigarray = array_unique($bigarray);
#print_r($bigarray);
foreach ($bigarray as $value)
{
	If ($value=="")
	{
	}
	else
	{
		echo "<a href=\"?mode=3&tag=".$value."\">".$value."</a><br />";
	}
}

?>


Report •
Related Solutions


Ask Question