0

Okay I have a query that looks like this :

   "SELECT `tag_name`,`sub_tag_name` FROM phpro_tag_targets targets
    INNER JOIN  sub_tags ON targets.sub_tag_id=sub_tags.sub_tag_id
    INNER JOIN phpro_tags tags ON targets.tag_id=tags.tag_id
    WHERE tag_target_id='{$vid['tag_target_id']}'";

The tables themselves looks like this:

phpro_tag_targets:

   tag_target_id | tag_id  | sub_tag_id | tag_target_name | tag_type_id
     int(11)     | int(11) | int(11)    |  var_char        | int(3)
    <PK - index> |   <fk>  |  <fk>    

phpro_tags:

  tag_id | tag_name
   <PK>  | VARCHAR
  sub_tags:
   sub_tag_id | sub_tag_name
     <PK>     | VARCHAR

and the result of the query looks something like this

Array ( 
    [0] => Array ( [tag_name] => Humanity [sub_tag_name] => Children ) 
    [1] => Array ( [tag_name] => Society [sub_tag_name] => Children ) 
    [2] => Array ( [tag_name] => Art and Artists [sub_tag_name] => Creativity ) 
    [3] => Array ( [tag_name] => Humanity [sub_tag_name] => Creativity ) 
    [4] => Array ( [tag_name] => Humanity [sub_tag_name] => Culture ) 
    [5] => Array ( [tag_name] => Society [sub_tag_name] => Culture ) 
    [6] => Array ( [tag_name] => unlisted [sub_tag_name] => Culture ) 
    [7] => Array ( [tag_name] => unlisted [sub_tag_name] => Culture ) 
)

The result is the tags and subtags associated with a piece of content (ie. video or article). I am attempting to display this information in html such that I have a <span class="tags"> and a <span class="subtags">. For the above eg. data the html would look like this:

   <span class="tags"> Humanity Society Art and Artists unlisted </span>
   <span class=subtags> Children Creativity Culture </span>

Is there a way that I can somehow filter data using php (or altering the query) so that I am only displaying Each different value a single time. As I am currently using a loop to cycle through the dat and displaying the same tag, and subtags multiple times, but can't figure out how to further filter the result.

An insight into how I could do this would be greatly appreciated!

4

2 回答 2

3

首先,您需要遍历数据数组并将所有标签名称和子标签名称收集到单独的数组中。然后使用array_unique从数组中删除重复值。最后使用implode将数组元素连接到字符串。

$tags = array();
$sub_tags = array();

// NOTE: $array is query result array
foreach ( $array as $value ) {
    $tags[] = $value['tag_name'];
    $sub_tags[] = $value['sub_tag_name'];
}

$tags = array_unique( $tags );
$sub_tags = array_unique( $sub_tags );

echo '<span class="tags">'.implode( ' ', $tags ).'</span>';
echo '<span class="subtags">'.implode( ' ', $sub_tags ).'</span>';

我真的希望我已经正确理解了这个问题。

于 2013-08-20T20:01:02.860 回答
0

这是您如何编辑查询以提供我认为您需要的单行结果的方法。带有 DISTINCT 选项和分隔符 ' ' 的 GROUP_CONCAT 函数应该为您提供所需的格式。

SELECT GROUP_CONCAT(DISTINCT `tag_name` SEPARATOR ' '),
       GROUP_CONCAT(DISTINCT `sub_tag_name`SEPARATOR ' ') 
FROM phpro_tag_targets targets
INNER JOIN sub_tags ON targets.sub_tag_id=sub_tags.sub_tag_id
INNER JOIN phpro_tags tags ON targets.tag_id=tags.tag_id
WHERE tag_target_id='{$vid['tag_target_id']}'

输出将是

tag_name | sub_tag_name
Humanity Society Art and Artists unlisted | Children Creativity Culture
于 2013-08-20T20:04:49.863 回答