1

我有一个带有 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; 

我还想用从上面获得的不同/唯一列表填充一个 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

4 回答 4

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;

希望这可以帮助。

于 2013-01-17T10:42:48.243 回答
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>
于 2013-01-17T10:42:55.643 回答
1

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

于 2013-01-17T10:41:42.427 回答
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;
?>

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

于 2013-01-17T11:42:11.303 回答