Check if an option already exists not to repeat it

Asked

Viewed 61 times

0

Hello!

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

I have a database where I have a product table in it. In this table, there is the column cor. So, I’m creating a filter to search the products on the site and since I don’t know exactly what colors are in stock, I did 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 one with this shape if I have registered the following colors: amarelo, verde, vermelho, amarelo, will appear 2 options containing the option amarelo.

What I want is that somehow, be checked if you already have that option in the select. If it has, it does nothing. If it does not, set the color to new option

1 answer

0


Try using this query:

$sql =  "SELECT DISTINCT `cor` FROM `produtos`";
  • I don’t know why you got a -1, it really worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.