php逗号分隔值的唯一/不同值

时间:2013-01-17 10:34:51

标签: php mysql

我有一个带有tag_filter字段的wallposts MySql表,该字段将具有逗号分隔值。

我想从此tag_filter字段获取用户的所有唯一/不同标记,并填充ul元素。

每个用户可能有很多帖子。

例如

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

所需的输出将是

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

到目前为止,这是我的代码:

$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; 

我还想用从上面获取的distinct / unique列表填充js var。

        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);  
         ?>
        ];

4 个答案:

答案 0 :(得分:3)

这可能会有所帮助。

$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;

希望这有帮助。

答案 1 :(得分:2)

MySQL字段中的逗号分隔值表示数据库设计错误,如果可能,应使用链接表。

但是,在您的情况下,以下代码可能有效:

$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";
}

将所有标记放在javascript变量中:

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

答案 2 :(得分:1)

尝试将所有tag_filter数组与array_merge()合并,然后使用array_unique()删除重复项。

答案 3 :(得分:0)

试试以下示例:

<?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;
?>

我认为这可以帮助您解决问题。