Solved

Option Group in PHP

Posted on 2013-02-03
7
719 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 dynamically set the form action using jQuery.

706 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

15 Experts available now in Live!

Get 1:1 Help Now