0

I have a problem ... I can not solve it for two days. I am a beginner in php.

i don't understand why while loop returns me as much selects fields as every select field has options, for example field "choose color" has 3 options, red green and yellow and i get instead one select field 3 of them... see this http://5dstudio.eu/select.jpg structure of my data base looks like this: http://5dstudio.eu/data.jpg

my php code:

<?php 
$sql = mysql_query("SELECT qty FROM attributes ORDER BY qty ");

while($row = mysql_fetch_array($sql)){
$name_attribute = $row["qty"];
$num = (int)$name_attribute;
    echo "<select>";
        $sql2 = mysql_query("SELECT name FROM attributes WHERE qty='$name_attribute' ORDER BY qty");
        while($row2 = mysql_fetch_array($sql2)){
        $value_attribute = $row2["name"];

        echo '<option>' ."$value_attribute". '</option>';

        }
    echo "</select>";
}
?>

Thanks for any tips and help!

2
  • mysql_* is deprecated. I understand you're new to PHP, but please change over to using mysqli as soon as possible. Otherwise, I don't understand your question. Commented Jan 5, 2014 at 19:52
  • You really shouldn't be making queries like this. Use mysqli instead. Commented Jan 5, 2014 at 19:52

1 Answer 1

1

Change your first query to this:

SELECT qty FROM attributes GROUP BY qty 

or

SELECT DISTINCT qty FROM attributes
Sign up to request clarification or add additional context in comments.

2 Comments

no problem. Do yo understand why you were getting that result? Also feel free to give me a check mark.
Yes I understand - MySQL by default returns all the values ​​regardless of whether they are the same or not, if you do not want to duplicate the results then let's use the DISTINCT operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.