Codded
Codded

Reputation: 1256

php unique/distinct value from comma seperated value

I have a wallposts MySql table with a tag_filter field, this field will have a comma seperated value.

I want to get all the unique/distinct tags for a user from this tag_filter field and populate in ul element.

There could be many posts per user.

eg

post1: tag_filter = a, b, c post2: tag_filter = a, d, e post3: tag_filter = c, d, e

the desired output would be

<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>

Here is my code thus far:

$tag_filter_list = mysql_query('SELECT tag_filter from wallposts where userid = '.$USER->id);

$list = "<ul>";

while ($tag_filter = mysql_fetch_array($tag_filter_list)) {
$value = $tag_filter["tag_filter"];
$info = explode(",", $value);
foreach( $info as $item ) {
$list.="<li>$item</li>\n";
}
}

$list .= "</ul>";
echo $list; 

I would also like to populate a js var with the distinct/unique list acquired from the above.

        var sampleTags = [
         <?php
                    $tag_filter_list = mysql_query('SELECT tag_filter from wallposts where userid = '.$USER->id);

                        $all_tags = array();
                        while ($tag_filter = mysql_fetch_array($tag_filter_list)) {
                          $value = $tag_filter["tag_filter"];
                          $info = explode(",", $value);
                          $all_tags = array_merge($all_tags, $info);
                        }
                        $tags = array_unique($all_tags); // values will be sorted by the function
                        foreach( $tags as $item ) {
                          $list = "$item";
                        }
                    echo join(',', $list);  
         ?>
        ];

Upvotes: 1

Views: 1110

Answers (4)

John Peter
John Peter

Reputation: 2928

Just try with the following example :

<?php
$post_array = array('a,b,c','a,d,e','c,d,e');
$tag_list   = '<ul>';
$i=0;
while($i < count($post_array)){
$filter_explode = explode(',',$post_array[$i]);
$filter_count   = count($filter_explode);
for($j=0;$j<$filter_count;$j++)
{
if(!in_array($filter_explode[$j],$tag_list_build)){
$tag_list_build[] = $filter_explode[$j];
}
}
$i++;
}
$tag_list_build_count = count($tag_list_build);
for($k=0;$k<$tag_list_build_count;$k++)
{
$tag_list.= '<li>'.$tag_list_build[$k].'</li>';
}
$tag_list.= '</ul>';
echo $tag_list;
?>

I think this may help you to resolve your problem.

Upvotes: 0

SirDarius
SirDarius

Reputation: 42899

Comma separated values in a MySQL field is a sign of a bad database design, linked tables should be used if possible.

However, in your case, the following code might work:

$all_tags = array();
while ($tag_filter = mysql_fetch_array($tag_filter_list)) {
  $value = $tag_filter["tag_filter"];
  $info = explode(",", $value);
  $all_tags = array_merge($all_tags, $info);
}
$tags = array_unique($all_tags); // values will be sorted by the function
foreach( $tags as $item ) {
  $list .= "<li>$item</li>\n";
}

To put all tags in a javascript variable:

<script type="text/javascript">
var sampleTags = [<?php echo explode(', ', $tags); ?>];
</script>

Upvotes: 2

web-nomad
web-nomad

Reputation: 6003

This might help.

$tag_filter_list = mysql_query('SELECT tag_filter from wallposts where userid = '.$USER->id);

$tags = array();
while ($tag_filter = mysql_fetch_array($tag_filter_list)) {
    $value = $tag_filter["tag_filter"];
    $tags[] = explode(",", $value); // put all in an array
}

// now, join and remove duplicates and sort in ascending order
$tags = implode( ',', $tags );
$tags = explode( ',', $tags );
$tags = array_values( array_unique( $tags ) );
sort($tags);

$list = "<ul>";

foreach( $tags as $item ) {
    $list .= "<li>$item</li>\n";
}

$list .= "</ul>";
echo $list;

Hope this helps.

Upvotes: 3

Lukas C
Lukas C

Reputation: 453

Try merging all tag_filter arrays with array_merge() and then use array_unique() to remove duplicates.

Upvotes: 1

Related Questions