如何用php / html动态设置选择值

时间:2015-06-02 13:13:17

标签: php html sql

我试图让它根据数据库中的$ rad [' brukertype']设置默认值,这可以使得eighter值为1或2但是如果声明似乎没有工作

 $sql = "select A.*, Br.Type from  Brukere A INNER JOIN Brukertyper Br ON A.Brukertype=Br.Brukertype";
        if ($db) {
            $res = $db->query($sql);
            while ($rad = $res->fetch_assoc()) {


     echo("<td><select name='type'>  <option value='1'"  if ($rad['Brukertype'] == '1') "selected='selected'" > "'Administrator</option> <option value='2'" if ($rad['Brukertype'] == '2') "selected='selected'">"Iskjører</option> </td>");

1 个答案:

答案 0 :(得分:1)

您的最后一行会导致错误。试试这个:

echo "<td><select name='type'> 
<option value='1'" . ($rad['Brukertype'] == '1' ? "selected='selected'" : '') . " >Administrator</option>
<option value='2'" . ($rad['Brukertype'] == '2' ? "selected='selected'" : '') . " >Iskjører</option>
</td>";