vanderlay Posted December 27, 2006 Share Posted December 27, 2006 Hello all and Happy Festivus!for each of you that can help me I will make a donation in your name to theHUMAN 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 itemsBrand Model MakeA i 1A ii 2B ii 3C i 4I would like to "SELECT DISTINCT Model FROM table" then group them by brand so the result would be,BRAND A Model i Model iiBRAND B model iiBrand C model ithe 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?thxArt Link to comment https://forums.phpfreaks.com/topic/31942-happy-festivus-and-returning-group-by-value/ Share on other sites More sharing options...
trq Posted December 27, 2006 Share Posted December 27, 2006 [code]SELECT DISTINCT Model FROM table GROUP BY brand[/code] Link to comment https://forums.phpfreaks.com/topic/31942-happy-festivus-and-returning-group-by-value/#findComment-148227 Share on other sites More sharing options...
Barand Posted December 27, 2006 Share Posted December 27, 2006 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] Link to comment https://forums.phpfreaks.com/topic/31942-happy-festivus-and-returning-group-by-value/#findComment-148230 Share on other sites More sharing options...
vanderlay Posted December 28, 2006 Author Share Posted December 28, 2006 bingo - thx Barand,works a charm.....can you also explain the difference between using '$var' and \"".$var."\" in an echo statement as this looks lie it could save me a few keystrokes every time I have to use it.Thx again Art Link to comment https://forums.phpfreaks.com/topic/31942-happy-festivus-and-returning-group-by-value/#findComment-148633 Share on other sites More sharing options...
Barand Posted December 28, 2006 Share Posted December 28, 2006 seehttp://www.php.net/manual/en/language.types.string.php#language.types.string.parsing Link to comment https://forums.phpfreaks.com/topic/31942-happy-festivus-and-returning-group-by-value/#findComment-148692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.