doujionggan9570 2015-07-13 12:04
浏览 53

如何设置所选选项

I'm trying to get selected option in select,I need to make a select from my database and use id to mark which category is set.

echo"<select>";
 while ($row = mysql_fetch_array($rezultat1))
{  if ($row['biljka_id']==$GET['b']) {
echo "<option value=".$row['biljka_id']." selected='selected' >".$row['naziv']."</option>";
} else { echo "<option value=".$row['biljka_id'].">".$row['naziv']."</option>";
}
echo"</select>";
  • 写回答

2条回答 默认 最新

  • dreamfly2016 2015-07-13 12:08
    关注

    Following is the short code.

    You need not use if else statement as you need to justify only one variable depending upon condition.

    You can do it with ternary operator.

    <?php
    echo "<select>";
    while ($row = mysql_fetch_array($rezultat1)) {
        $selected = ($row['biljka_id']==$GET['b']) ? 'selected="selected"' : '';
        echo "<option value=".$row['biljka_id']. $selected ">".$row['naziv']."</option>";
    }
    echo"</select>";
    ?>
    

    Rererence

    评论

报告相同问题?