Frage

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!

War es hilfreich?

Lösung

First you need to loop through the data array and collect all tag names and subtag names into separate arrays. Then use array_unique to remove duplicate values from an array. And finally use implode to join array elements to a string.

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

I really hope that I have correctly understood the question.

Andere Tipps

Here is how you can edit your query to give you a one line result with what I think you need. The GROUP_CONCAT function with the DISTINCT option and a SEPARATOR of ' ' should give you the format you are going for.

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']}'

The output for this will be

tag_name | sub_tag_name
Humanity Society Art and Artists unlisted | Children Creativity Culture
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top