Dynamic dropdown with <optgroup>

flyboeing 01 Dec, 2011
Hello all,

I have a list om different aircraft. This list contains civil and military aircraft. Now I have the idea to group them by section (civil/military).

I have the following code:

<?php
$db =& JFactory::getDBO();
$count = $db->loadResult();

$query = "
SELECT DISTINCT `aircraft`, `section`
FROM `movements_ewas` ORDER BY `aircraft`;
";
$db->setQuery($query);
$data = $db->loadObjectList();
?>
<select style='width: 210px;' class='cf_inputbox validate-selection' name='aircraft' id='select' size='1'>
<option value=''>Kies een toestel</option>

<?php
foreach ( $data as $d ) {
$selected = '';
if ( $d->id == $aircraft ) {
$selected = "selected='selected'";
}
echo "<optgroup label='".$d->section."'>";
echo "<option value='".$d->aircraft."'>".$d->aircraft."</option>";
echo "</optgroup>";
}
?>
</select>


This is the result I get:
Dynamic dropdown with <optgroup> image 1

As you can see, it shows the section for each aircraft, but it ain't grouped. I tried several things but I can't get it to work.

Does someone has an idea?🙂
GreyHead 01 Dec, 2011
Hi flyboeing,

I think that the PHP in the middle needs to be a little cleverer
<?php
$optgroup = '';
foreach ( $data as $d ) {
  $selected = '';
  if ( $d->id == $aircraft ) {
    $selected = "selected='selected'";
  }
  if ( $d->section != $optgroup ) {
    if ( $optgroup ) {
      echo "</optgroup>";
    }
    echo "<optgroup label='{$d->section}'>";
    $optgroup = $d->section;
  }
  echo "<option value='{$d->aircraft}' $selected >{$d->aircraft}</option>";
}
echo "</optgroup>";
?>
NB Not tested and may need debugging

This should only insert an <optgroup> tag when the optgroup changes.

Bob
flyboeing 01 Dec, 2011
Thank you very much Bob!
I only have question/problem. When I go to my searchpage, The "standard" selection is not "Select an aircraft", but it is a aircraft from the list. How can I change that to the "Select an aircraft" text?
GreyHead 01 Dec, 2011
Hi flyboeing,

Have you removed this line?
<option value=''>Kies een toestel</option>


If so put please put it back again.

Bob
flyboeing 01 Dec, 2011
Hi Bob,

This is my code:
<?php
$db =& JFactory::getDBO();
$count = $db->loadResult();

$query = "
SELECT DISTINCT `aircraft`, `section`, `type`
FROM `movements_ewas` ORDER BY `section`, `aircraft`;
";
$db->setQuery($query);
$data = $db->loadObjectList();
?>
<select style='width: 210px;' class='cf_inputbox validate-selection' name='aircraft' id='select' size='1'>
<option value=''>Kies een toestel</option>

<?php
$optgroup = '';
foreach ( $data as $d ) {
  $selected = '';
  if ( $d->id == $aircraft ) {
    $selected = "selected='selected'";
  }
  if ( $d->section != $optgroup ) {
    if ( $optgroup ) {
      echo "</optgroup>";
    }
    echo "<optgroup label='{$d->section}'>";
    $optgroup = $d->section;
  }
  echo "<option value='{$d->aircraft}' $selected >{$d->aircraft} ({$d->type})</option>";
}
echo "</optgroup>";
?>
</select>  


I haven't removed that line. I just replaced the original php code in the middle with the one you placed here.
GreyHead 01 Dec, 2011
Hi flyboeing,

That looks fine to me. Maybe be a little typo has crept in somewhere? Please post a link to the form so I can take a quick look.

Bob
GreyHead 01 Dec, 2011
Hi flyboeing,

Looking at the form the last entry is being selected. This line is wrong:
  if ( $d->id == $aircraft ) {
Neither $d->id or $aircraft are defined so it it always true.

Probably my mistake as I added the $selected into the option tag.

Bob
flyboeing 01 Dec, 2011
Thank you Bob for your help!😀
I'll buy you a beer!😀
This topic is locked and no more replies can be posted.