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>";
   } 
   ?>

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

Archived

This topic is now archived and is closed to further replies.

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