Select option selected an not selected

foreach, php, selected

Solution

use this code

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

Problem

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?

Original source