Hello I use the code bellow to get some content out of one of my tables in the database. each row in the database has it's own "tag" but multiple rows can have the same tag. so currently when i run the code bellow i could get 123 cat animal 124 cow animal 125 tree forest where "animal" and "forest" being the tags. i wonder how i could make it so that i only se the "animal" tag once and not several times if several collumns have this tag when i do this output. is this possible ? <? $query = "SELECT COUNT( * ) AS `Rader` , `tags`FROM `images`GROUP BY `tags`ORDER BY `tags`"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['tags']; echo "<br>"; } //} ?> PHP:
but the query you're using should do excatly the thing, it is grouping by tag so it's resultset is Rader tags 2 animal 1 forest so your code will output animal<br/>forest. If you need only the disctinct values from tags column and don't need their counts, you can use: SELECT DISTINCT tags FROM images;
The problem is more that "animal" is within a larger string and any query solution I give you will run like a dog. You need a table of possible tags and a linking table joining the 'rader' and the tags. This is called normalising - and its essential if you want to be able to query on your database.
I just realized that it is single column with the text like 'cat animal' and 'cow animal' after sarahk's post Yes, this is not good and consider DB model change, but to estimate the performance: $query = "SELECT * FROM images"; $tags = array(); $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ foreach(explode(' ',$row['tags']) as $tag) { $tags[] = $tag; } } $tags = array_unique($tags); foreach($tags as $tag) { echo $tag.'<br/>'; } //} PHP:
Ye the script came this way and im thinking of getting the database reworked. however Bytes. with yours code the output is the following 123 Bleach,Anime,Wallpaper 124 Code Geass,Anime,Wallpaper 125 D.Gray-Man,Anime,Wallpaper is is possible to filter away "Wallpaper" and the many others that that have multiple resaults so "Wallpaper" is only shown once like the example bellow ?, 123 Bleach,Anime,Wallpaper 124 Code Geass 125 D.Gray-Man
could you copy here a sample of your data with headers, so table structure + few rows? Because it is still unclear to me where the IDs come from and what separator (comma or space) is used. Thanks