[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Option Group in PHP

Posted on 2013-02-03
7
Medium Priority
?
964 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
[X]
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
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 111

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 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