Link to home
Start Free TrialLog in
Avatar of DS928
DS928Flag for United States of America

asked on

Option Group in PHP

I have a combo box that I want to group.  When I had it hard coded it worked.  But I can't seem to figure it out with this query.  This is what I had before.

<select name="Filters" class="Filters" id="Filters">
	    <option selected="selected">--Select Filter--</option>
    <optgroup label="FEATURES">
      <option value="1">Buffet
        <option value="2">Fireplace</option>
      <option value="3">Great Views</option>
      <option value="4">Outdoor Dining</option>
            </optgroup>
    
    <optgroup label="MEAL PERIODS">
      <option value="10">Breakfast</option>	
      <option value="11">Brunch</option>	
      <option value="12">Dinner</option>	
      <option value="13">Happy Hour</option>	
           </optgroup>
    
    <optgroup label="SERVICES">
      <option value="18">BYOB
        <option value="19">Catering</option>
      <option value="20">Credit Cards</option>
      <option value="21">Delivery</option>
           </optgroup>
    
    <optgroup label="TYPE OF PLACE">
      <option value="27">Bar Scene</option>
      <option value="28">Business Dining</option>
      <option value="29">Cheap Eats</option>
etc..................

Open in new window


This is what I have now.  Its not grouping at all.  Also its placing a space between each row.

<?php
require('config.php');
$Doggie=$_POST['Doggie'];
$Kitty=$_POST['Kitty'];
$Pig=$_POST['Pig'];

$sql = mysql_query("SELECT tblDetails.DetailID as DID, tblDetails.DetailType,
		CONCAT(tblDetails.DetailName,'    (', Count(tblLocDet.DetailsID),')') as DOG
		FROM (tblLocations INNER JOIN tblLocDet ON tblLocations.LocationID = tblLocDet.LocationID)
		INNER JOIN tblDetails ON tblLocDet.DetailsID = tblDetails.DetailID
		GROUP BY tblDetails.DetailID, tblDetails.DetailType, tblDetails.DetailName, tblLocations.CityID, 
		tblLocations.AreaID, tblLocations.CuisineID
		HAVING tblLocations.CityID='16'
		AND tblLocations.AreaID='131'
		AND tblLocations.CuisineID='3'
		ORDER BY tblDetails.DetailType, tblDetails.DetailName");
				echo '<option selected="selected">--Filter Your Results--</option>';
				while($row=mysql_fetch_array($sql))
				{
				echo '<optgroup label=>'.$row['DetailType'].'</optgroup>';
				echo '<option value="'.$row['Did'].'">'.$row['name'].'</option>';
				}
?>

Open in new window

Avatar of DS928
DS928
Flag of United States of America image

ASKER

Tried this...Still no Group Showing...

echo '<optgroup label=>'.$row['DetailType']. '<option value="' .$row['Did'].'">'.$row['DOG'].'</option></optgroup>';

Open in new window

Not sure I can debug all of that code, but I can see one thing that might be helpful.  Array indexes are case-sensitive.  If the script runs a query to SELECT... as DID and PHP looks for Did, there will not be anything found.

Please, please put error_reporting(E_ALL) in the top of all of your scripts and correct any Notice conditions that arise.  This may not be the only thing wrong (I suspect it's not) but at least it will get past one of the issues and maybe help you find the others.
Avatar of DS928

ASKER

Thats not in. its the syntax.  This works with HTML apparently the syntax for PHP is different.  Still cant find the correct syntax.  It works without the grouping. I just need to add the grouping.  Thank you Ray.
You need to wrap your options within the optgroup tags in order to make the grouping take effect. http://www.w3schools.com/tags/tag_optgroup.asp

This is what you tried beforehand but it has some little html syntax errors in it:

echo '<optgroup label=>'.$row['DetailType']. '<option value="' .$row['Did'].'">'.$row['DOG'].'</option></optgroup>';

Open in new window


Try this modified version:
echo '<optgroup label="'.$row['DetailType'].'"><option value="' .$row['Did'].'">'.$row['DOG'].'</option></optgroup>';

Open in new window

Avatar of DS928

ASKER

Thank you.  I am now seeing the Detail Types.  However instead of...
Features
      Fireplace
      Wifi
      Raw Bar

I am seeing.
Features
      Fireplace
Features
      WiFi
Features
      Raw Bar

How can this be solved?  Once again.  Thank you.
That's because on every iteration you're wrapping one option into the optgroup tags.

You'll probably need to do separate db queries for grouping them.

So you will call select all options for one group.
Then the next options for the next group and so on.

Making an iteration which makes your optgrouping work is a little too complex and you should consider to hier a professional to create a method which is capable of that.

Best regards,
mcnute
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial