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