Reputation: 7
I'm having this following piece of code which populates a dropdown box based on the country column in my database. This works perfectly fine.
echo "<select name='pob_country' id='pob_country' data-native-menu='false'>";
echo "<option>Country</option>";
while ($row_country = mysql_fetch_array($result_countries)) {
echo "<option value='".
$row_country['country'] ."'>".
$row_country['country'] .
"</option>";
}
echo "</select>";
Now I want to set one <option>
to selected
based on a variable.
I tried it with a tenary operator, like so:
echo "<select name='pob_country' id='pob_country' data-native-menu='false'>";
echo "<option>Country</option>";
while ($row_country = mysql_fetch_array($result_countries)) {
echo "<option value='".
$row_country['country'] ."'".
(($pob_country=="$row_country['country']") ? "selected" : "") .
">".
$row_country['country'] .
"</option>";
}
echo "</select>";
Somehow this doesn't work, the page doesn't load. I don't understand what I'm doing wrong here. It probably is something really simple but I'm stuck on it for over an hour.
Any help is appreciated.
Upvotes: 0
Views: 59