user3163228
user3163228

Reputation: 3

php How to sort and brake while loop based on variable

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!

Upvotes: 0

Views: 102

Answers (1)

Frank Conry
Frank Conry

Reputation: 2718

Change your first query to this:

SELECT qty FROM attributes GROUP BY qty 

or

SELECT DISTINCT qty FROM attributes

Upvotes: 1

Related Questions