Jump to content

Adding Another Field To PHP Option Group Dropdown


DSTR3

Recommended Posts

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
Share on other sites

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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.