Scan table row for unique values

Discussion in 'PHP' started by lruneh, Dec 18, 2008.

  1. #1
    Hi all

    I have a mysql database with a table. What I'd like to do is to scan a speciffic row in that table for unique values and put the values in an array. If, fx, the value gimp occurs twice in the row, only the first occurrence should be put in the array.

    Does anyone have a sollution to this, that they would like to share?

    thanks
     
    lruneh, Dec 18, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    SELECT DISTINCT(FieldName) from TableName;
     
    crivion, Dec 18, 2008 IP
  3. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $rst=mysql_query($sqlstatement);
    
    while($row_of_fields=msql_fetch_assoc($rst)) {
        foreach($row_of_fields as $key=>$value) {
            if(!$check_array[$value]) {
                $use_array[$key]=$value;
                $check_array[] = $value;
            }
        }
    //do something with $use_array
    unset($check_array);
    }
    
    PHP:
    takes a tablerow as associative array $key (=fieldname), $value
    tests against $check_array to see if $value is missing
    if so,
    add $key, $value to $use_array
    add $value to $check_array
    if $value isn't missing from $check_array, it skips to the next field

    Haven't tested it.
     
    juust, Dec 18, 2008 IP
  4. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi guys,
    Thank you very much - both replies were very helpful!
     
    lruneh, Dec 20, 2008 IP