rvroo
rvroo

Reputation: 7

Issue with ternary operator in echo

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

Answers (1)

The Blue Dog
The Blue Dog

Reputation: 2475

Remove the quotes:

($pob_country==$row_country['country'])

Upvotes: 1

Related Questions