php mysql question

Discussion in 'PHP' started by izlik, Apr 27, 2010.

  1. #1
    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:

     
    izlik, Apr 27, 2010 IP
  2. bytes

    bytes Peon

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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;
     
    bytes, Apr 27, 2010 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #3
    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.
     
    sarahk, Apr 27, 2010 IP
  4. bytes

    bytes Peon

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    bytes, Apr 27, 2010 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    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
     
    izlik, Apr 29, 2010 IP
  6. bytes

    bytes Peon

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    bytes, Apr 29, 2010 IP