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 Quote 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] Quote 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] Quote 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 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.