Solved

Option Group in PHP

Posted on 2013-02-03
7
731 Views
Last Modified: 2013-02-07
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

0
Comment
Question by:DS928
7 Comments
 

Author Comment

by:DS928
ID: 38849303
Tried this...Still no Group Showing...

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

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38849619
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.
0
 

Author Comment

by:DS928
ID: 38849643
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.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 11

Expert Comment

by:mcnute
ID: 38850354
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

0
 

Author Comment

by:DS928
ID: 38851448
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.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38851525
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
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38863080
Regarding the other question:

<select name="DETAILS">
<?php
include("config.php");



$sql = "SELECT DetailType AS type, GROUP_CONCAT(DISTINCT DetailName ORDER BY DetailName DESC 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>"; 
}  
?>
</select> 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now