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

I have a form that has this <%= submit_tag 'Search Event' %> and a
I have a form that is going to be used to search through a
I have SP that selects data from a table with two columns like this
I have a search form in my web application that throws an Apache 400
I have a form that lets a user perform a search for a stock
I have a small issue on a search form. PLease check this: http://www.fortisfitness.ca/test/script.php What's
I have made an normal form that you can enter a user´s fullname in
My idea: I have a database of users, each user has an autoincremented id
I currently have a search box in our admin navigation that is submitting to
I have three simple html or php file that I'm going to create. What

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.