Check if an option already exists for not repeating it

0

Hello

I have a maybe simple problem, but I can not think of any way to solve it.

I have a database where I have a table of products on it. In this table, you have the cor column. So I'm creating a filter when searching the products on the site and as I'm not sure exactly what colors are in stock, I made this while to print the options:

$sql =  "SELECT 'cor' FROM 'produtos'";
<select id='iCor' name='nCor'>
       <option value=''>Todos</option>";
       while($linha = $result->fetch_assoc()){
            $cor = $linha['cor'];
            echo"<option value='$cor'>$cor</option>";
       }'

But this with this form if I have registered the following colors: amarelo , verde , vermelho , amarelo , will appear 2 options containing option amarelo .

What I want is that somehow, be checked if you already have that option no select . If you have, you do nothing. If you do not, put the color as new option

    
asked by anonymous 03.10.2016 / 22:20

1 answer

0

Try to use this query:

$sql =  "SELECT DISTINCT 'cor' FROM 'produtos'";
    
03.10.2016 / 22:36