Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:59:11+00:00 2026-05-23T01:59:11+00:00

I have a search form that searches a table in my database. The table

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:59:12+00:00Added an answer on May 23, 2026 at 1:59 am

    First of all, modify the query to sort by country first, then icao.

    public function airportstuff() {
        $query = "SELECT icao, name, country
                FROM phpvms_airports
                ORDER BY country, icao";
        return DB::get_results($query);
    }
    

    Then try this:

    $previousCountry = null;
    foreach ($airports as $airport) {
        if ($previousCountry != $airport->country) {
            // close the previous optgroup tag only if this isn't our first time through the loop
            if ($previousCountry !== null) {
                echo '</optgroup>';
            }
            echo '<optgroup label="'.$airport->country.'">';
        }
        echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>';
        $previousCountry = $airport->country;
    }
    // close the final optgroup only if we went through the loop at least once
    if ($previousCountry !== null) {
        echo '</optgroup>';
    }
    

    BTW: To avoid XSS vulnerabilities in your code, you really should wrap all dynamic parts of the HTML you’re building in htmlspecialchars() or some similar function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Using MySQL and PHP) I have a search form that will allow my users
I have a form that searches a MySQL database using PHP. Currently, when a
The functionality I'm looking for: I have a form that will search my 'Proposal'
I have written a simple mysql search form that searches between two dates. This
I have a table homes which has country,city,continent fields. When some searches for a
I have a search form on my site that submits the url in the
I have a form that submits a search for blogs on my site via
Let us say that I have a form to search by multiple tags in
I have a search form that the user can search for an Article content
I've built this little script that searches a table in my database, but for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.