填充同一个表中的多个选择框

时间:2013-11-08 09:01:39

标签: php html-select

我想用相同的项目(从一个数据库表)填充三个选择框。 这是我到目前为止的代码。我想避免三个问题。你能帮我解决一下填充多个选择框的问题吗?

<select name="resort_3" class="swcomp" data-index="3">
<?php   
$today = date("Y-m-d");
$sQuery2 = "SELECT DISTINCT res_id, resort from sv_snow WHERE lud='$today' ORDER by resort";
$sResult2 = mysql_query($sQuery2) or die('Error, query 2 failed');

while($ro = mysql_fetch_assoc($sResult2))
{
 echo '<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> '; 
}
?>
</select>

1 个答案:

答案 0 :(得分:2)

将选项保存在字符串中,然后将这些选项分配给所有3个选择框。很简单

<?php
while($ro = mysql_fetch_assoc($sResult2))
{
 $options.='<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> '; 
}
?>


<select name="resort_1" class="swcomp" data-index="1">
<?php echo $options; ?>
</select>

<select name="resort_2" class="swcomp" data-index="2">
<?php echo $options; ?>
</select>

<select name="resort_3" class="swcomp" data-index="3">
<?php echo $options; ?>
</select>