I have a search form that searches for a table in my database. The table has the following columns:
icao - name - country
I have php that will put these fields in the selection options for each row with the following query:
public function airportstuff() {
$query = "SELECT icao, name, country
FROM phpvms_airports
ORDER BY icao";
return DB::get_results($query);
}
Then php looks like this:
<?php foreach ($airports as $airport)
{echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';}
?>
This works fine, but I want to be able to use the country column as an optgroup, so each country is an optgroup, with the corresponding airports for that country in the right optgroup.
I tried this:
<?php
foreach ($airports as $airport)
{
echo '<optgroup label="'.$airport->country.'">';
echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';
?>
<?php echo '</optgroup>';}?>
But he just creates an optgroup for each airport.
Any help on this subject would be greatly appreciated, and thanks in advance.
Stewart
source
share