下拉列表和PHP中的MySQL调用

时间:2012-05-16 11:01:22

标签: php mysql selection

我找到了一个关于如何从数据库中获取数据以选择在PHP中保存到数据库中的值的模型,但它不起作用,有人可以就以下代码提出建议吗?

     <?php      
             $tempoSEL = array("","","","","","","","","","");
if($row['tempo'] == "1"){
$tempoSEL[1] = " selected='selected' ";
}else if($row['tempo'] == "2"){
$tempoSEL[2] = " selected='selected' ";
}else if($row['tempo'] == "3"){
$tempoSEL[3] = " selected='selected' ";
}else if($row['tempo'] == "4"){
$tempoSEL[4] = " selected='selected' ";
}else if($row['tempo'] == "5"){
$tempoSEL[5] = " selected='selected' ";
}else if($row['tempo'] == "6"){
$tempoSEL[6] = " selected='selected' ";
}else if($row['tempo'] == "7"){
$tempoSEL[7] = " selected='selected' ";
}else if($row['tempo'] == "8"){
$tempoSEL[8] = " selected='selected' ";
}else if($row['tempo'] == "9"){
$tempoSEL[9] = " selected='selected' ";
}
$db = PageDB::getInstance();

?>
              <p>
                <label style="padding-right:28px;">Tempo</label>
                <select name="tempo">
                  <option value="1" <?php echo $tempo[1]; ?>>2 Sec</option>
                  <option value="2" <?php echo $tempo[2]; ?>>5 Sec</option>
                  <option value="3" <?php echo $tempo[3]; ?> selected>10 Sec</option>
                  <option value="4" <?php echo $tempo[4]; ?>>20 Sec</option>
                  <option value="5" <?php echo $tempo[5]; ?>>30 Sec</option>
                  <option value="6" <?php echo $tempo[6]; ?>>45 Sec</option>
                  <option value="7" <?php echo $tempo[7]; ?>>60 Sec</option>
                  <option value="8" <?php echo $tempo[8]; ?>>90 Sec</option>
                  <option value="9" <?php echo $tempo[9]; ?>>120 Sec</option>              
                </select>

1 个答案:

答案 0 :(得分:0)

<?php 
$a=array("","2","5","10","20","30","45","60","90","120");     
$db = PageDB::getInstance();
            echo  '<p><label style="padding-right:28px;">Tempo</label>
                <select name="tempo">';
for($i=1;$i<10;$i++)
echo '<option value="'.$i.'" '.($row['tempo']==$i?'selected':'').'>'.$a[$i].'Sec</option>'
echo '</select>';
?>

可能它可以帮到你 的更新:

    <?php 
$a=array("","2","5","10","20","30","45","60","90","120");     
$d=3;
            echo  '<p><label style="padding-right:28px;">Tempo</label>
                <select name="tempo">';
for($i=1;$i<10;$i++)
{echo '<option value="'.$i.'" '.($d==$i?'selected':'').'>'.$a[$i].'Sec</option>';if($d==$i)$a[0]=$d;}
echo '</select>';
?>

$ a [0]将存储默认值