Question

I'm trying to Update a SQL Field so what i got is a Select and

$row = $stmt->fetch(PDO::FETCH_ASSOC);
....
$country_ausgabe = $row['country'];

So country_ausgabe as for example a value = de for german.

Also i have a array with all country codes in it. So with this code i get all in a Select. $countries_de = the array with all country codes.

<select>
foreach ($countries_de as $key=>$country) {
echo "<option value='" . $key . "'>" . $country . "</option>";}
</select>

So but how can i use my $country_ausgabe to set this value in the select to selected?

Was it helpful?

Solution

use this code

<select>
foreach ($countries_de as $key=>$country) {
$selected = ($country_ausgabe == $key)?"SELECTED":""; 
echo "<option value='" . $key . "' ".$selected." >" . $country . "</option>";}
</select>

OTHER TIPS

try this

echo "<select>";
foreach ($countries_de as $key=>$country) 
{
   $selected="";
   if($key==$country_ausgabe) 
   {
      $selected = "selected";
   }
   echo "<option value='" . $key . "' " . $selected . " >" . $country . "</option>";
}
echo "</select>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top