Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Option Group in PHP

Posted on 2013-02-03
7
757 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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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