DSTR3 Posted February 10, 2013 Share Posted February 10, 2013 I need to add DetailID to this. I am having a bit of trouble because this is an Option Group. Any help is appreciated. I need the DetailID to be first, then DetailName, but you should not see the DetaiID, just the DetailName. Thank you. <?php include("config.php"); //This builds the dropdown based on City, Area, and Cuisiner for the results_city.php file $sql = "SELECT DetailType AS type, DetailID, GROUP_CONCAT(DISTINCT DetailName ORDER BY DetailName ASC SEPARATOR '|') AS DetailName FROM tblDetails GROUP BY DetailType"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo "<optgroup label='{$row['type']}'>"; $DetailNames = explode('|', $row['DetailName']); foreach($DetailNames as $DetailName) { echo "<option value='".$DetailName."'>".$DetailName."</option>"; } echo "</optgroup>"; } ?> Link to comment https://forums.phpfreaks.com/topic/274294-adding-another-field-to-php-option-group-dropdown/ Share on other sites More sharing options...
Christian F. Posted February 10, 2013 Share Posted February 10, 2013 Replace the $detailName in the value with the $detailID. You don't need to send both. Link to comment https://forums.phpfreaks.com/topic/274294-adding-another-field-to-php-option-group-dropdown/#findComment-1411550 Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 try $sql = "SELECT DISTINCT DetailType AS type, DetailID, DetailName FROM tblDetails ORDER BY DetailType, DetailName"; $result = mysql_query($sql) or die(mysql_error()); $prev=''; while ($row = mysql_fetch_assoc($result)) { if ($prev != $row['type']) { if ($prev) echo "</optgroup>"; echo "<optgroup label='{$row['type']}'>"; $prev = $row['type']; } echo "<option value='".$row['DetailID']."'>".$row['DetailName']."</option>"; } echo "</optgroup>"; Alternatively you could GROUP_CONCAT(DISTINCT CONCAT(DetailID, ',', DetailName) ORDER BY DetailName ASC SEPARATOR '|') AS DetailName and do a double explode but I think the above is simpler Link to comment https://forums.phpfreaks.com/topic/274294-adding-another-field-to-php-option-group-dropdown/#findComment-1411552 Share on other sites More sharing options...
DSTR3 Posted February 10, 2013 Author Share Posted February 10, 2013 PERFECT! This headache is over! Thank you very much! Link to comment https://forums.phpfreaks.com/topic/274294-adding-another-field-to-php-option-group-dropdown/#findComment-1411590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.