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:
This is the result I get:

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?🙂
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:

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?🙂
Hi flyboeing,
I think that the PHP in the middle needs to be a little cleverer
This should only insert an <optgroup> tag when the optgroup changes.
Bob
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 debuggingThis should only insert an <optgroup> tag when the optgroup changes.
Bob
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?
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?
Hi flyboeing,
Have you removed this line?
If so put please put it back again.
Bob
Have you removed this line?
<option value=''>Kies een toestel</option>
If so put please put it back again.
Bob
Hi Bob,
This is my code:
I haven't removed that line. I just replaced the original php code in the middle with the one you placed here.
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.
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
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
Hi flyboeing,
Looking at the form the last entry is being selected. This line is wrong:
Probably my mistake as I added the $selected into the option tag.
Bob
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
This topic is locked and no more replies can be posted.