skip to Main Content

I have a select field that gets its values from a database table. Not all records in the database have all fields. Some are null. I want my select field to show the name,iata,icao – a combination of these three seperated by a comma. The issue is that some of my fields do not have an icao and some do not have an iaata. Is there a way for me to alter my code such that if there is no icao or iata the comma is ignored.

Please see below:

enter image description here

enter image description here

The code i need to alter is below:

 <select name="airport1" class="airport1" id="airport1">
                    <option selected value="Add">Select Airport</option>

                    <?php
                    $mysqli = NEW MYSQLI("localhost", "root", "", "airports");
                    $resultSet = $mysqli->query("SELECT name,iata,icao 
    FROM airports order by name ASC;
    ");

?>

                    <?php
                    while ($rows = $resultSet->fetch_assoc()){
                        $name = $rows['name'];
                        $iata = $rows['iata'];
                        $icao = $rows['icao'];
                        echo "<option value='$name,$iata,$icao'>$name,$iata,$icao</option>";
                    }
?>

3

Answers


  1. You can check whether iata or icao is null before appending them to the string:

    ...
    while ($rows = $resultSet->fetch_assoc()) {
        $name = $rows['name'];
        $iata = $rows['iata'];
        $icao = $rows['icao'];
    
        $displayValue = $name;
        
        if ($iata) {
            $displayValue .= ", " . $iata;
        }
        
        if ($icao) {
            $displayValue .= ", " . $icao;
        }
    
        echo "<option value='$name,$iata,$icao'>$displayValue</option>";
    }
    ...
    

    OR: You can use implode function to join the non-null values with a comma

    ...
    while ($rows = $resultSet->fetch_assoc()) {
        $name = $rows['name'];
        $iata = $rows['iata'];
        $icao = $rows['icao'];
    
        //create an array of non-null values
        $nonNullValues = array_filter([$name, $iata, $icao]);
    
        //use implode to join the non-null values with a comma
        $displayValue = implode(', ', $nonNullValues);
    
        echo "<option value='$name,$iata,$icao'>$displayValue</option>";
    }
    ...
    
    Login or Signup to reply.
  2. What you could do is put those values in a list

    $options = [$name,$iata,$icao];
    

    then you can remove the "empty" values

    $options = array_filter($options);
    

    and use them to populate what you need to populate, example:

    echo "<option value='". implode(',', $options) ."'>...";
    
    Login or Signup to reply.
  3. You can use trim to remove trailing commas and a regex to replace if multiple comma consecutive (in this case, only field iata is null):

    while ($rows = $resultSet->fetch_assoc()){
        $name = $rows['name'];
        $iata = $rows['iata'];
        $icao = $rows['icao'];
        $option = "$name,$iata,$icao";
        $option = trim($option,",");
        $option = preg_replace("/,{2,}/",",",$option );
        echo "<option value='$option '>$option </option>";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search