I have a search form that searches a table in my database. The table has the following columns:
icao - name - country
I have the php that will place these fields in select 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 the php is as follows:
<?php foreach ($airports as $airport)
{echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';}
?>
This works OK, but I want to be able to use the country column as an optgroup, so each country is an optgroup, with the respective airports for that country in the right optgroup.
I've 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 it just creates an optgroup for every airport.
Any help on this would very much be appreciated, and thanks in advance.
Stuart