Jump to content

Happy Festivus! - and returning "group by" value


vanderlay

Recommended Posts

Hello all and Happy Festivus!

for each of you that can help me I will make a donation in your name to the
HUMAN FUND, money for people.

let us celebrate Festivus, a true holiday for the rest of us.

but before we move on to the The Airing of Grievances, 'cause the deity of your choice knows "I got a lot of problems with you people!", can you take a look at this problem and help me out.


I am am trying to generate a SELECT box and group the items using echo "optgroup". In my table I have several items

Brand  Model  Make
A        i          1
A        ii          2
B        ii          3
C        i          4

I would like to "SELECT DISTINCT Model FROM table" then group them by brand so the result would be,
BRAND A
  Model i
  Model ii
BRAND B
  model ii
Brand  C
  model i

the first part of the select statement works fine and i rtn the values using,
[code]
while ($p= mysql_fetch_array($query))
      {
      echo "<option value=\"".$p[model]."\">".$p[model]."</option>";
      }
[/code]
  but how do i get the brand value and rtn it for the optgroup?

thx
Art
try

[code]
<?php

$sql = "SELECT DISTINCT brand, model FROM table
        ORDER BY brand, model";
$res = mysql_query($sql) or die(mysql_error());
$prev = '';

echo "<SELECT name=model>\n";

while (list($brand, $model) = mysql_fetch_row($res)) {
    if ($prev != $brand)  {
        if ($prev != '') {                      // only if its not first brand
            echo "</OPTGROUP>\n";
        }
        echo "<OPTGROUP label='$brand'>\n";
        $prev = $brand;
    }
    echo "<option value='$model'>$model</option>\n";
}
echo "</OPTGROUP>\n";
echo "</SELECT>\n";
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.