0

我有一个向我的数据库提交 2 个值的表单,当表单加载时,我检查数据库中是否已有值,我一直在尝试使用这些值并将它们设置为我的下拉框中的默认值。任何帮助,将不胜感激。谢谢

<form method="post" action="add_res.php?id=<? echo $id;?>">
    <fieldset>
    <label>From</label>
    <select name='one' id='one'>
    <?
        for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
            for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
                echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
    ?>
    </select><br><br>
    <select name='two' id='two'>
    <?
        for($hours=0; $hours<24; $hours++)
            for($mins=0; $mins<60; $mins+=30)
                echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
    ?>
    </select>
    </fieldset>
    <input type="submit" value="Restrict" class="btn_searchp"></a>
</form>
4

1 回答 1

0

您必须组合该值,然后与数据库值进行比较

<select name="one" id="one">
<?php for loop
$timeToDisplay = sprintf('%s:%s',str_pad($hours,2,'0',STR_PAD_LEFT),
                                 str_pad($mins,2,'0',STR_PAD_LEFT));
$selected = if($row['timeRow'] == $timeToDisplay ) ? 'selected="selected"' : '';
// let $row['timeRow'] is the value retrieve from database 
?>
   <option value="<?=php echo $timeToDisplay;?>" <?php echo $selected;?> >
     <?php echo $timeToDisplay?>
   </option>
</select>

参考 :

sprintf

ternary operator

于 2012-07-09T04:16:58.983 回答