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>"; } ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 (edited) 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 Edited February 10, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
DSTR3 Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) PERFECT! This headache is over! Thank you very much! Edited February 10, 2013 by DSTR3 Quote Link to comment 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.