简体   繁体   English

PHP选择(选项列表)

[英]Php select (Option list)

I am using these codes 我正在使用这些代码

$query = "SELECT date  FROM arrival order by date" ; 
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;  
echo "<select name='per1' id='per1'>";

while ($row = mysqli_fetch_array ($result)){?>
<option value=<?php echo $row['date'];?> selected='selected'><?php echo $row['date'];?></option>
</select>
<?php
}

?>

The select (Option list) displays data as 选择(选项列表)将数据显示为

2016-07-01
2016-07-02
2016-07-03
2016-07-04
2016-07-05
2016-07-06
and so on

It is not like a dropdown option list. 它不像一个下拉选项列表。 How to make it like dropdown list? 如何使其像下拉列表?

I want select one date from option. 我想从选项中选择一个日期。

You are closing the select in the while multiple times. 您将在while时间内多次关闭select Move </select> outside. </select>移到外面。

}?> </select>

You also should quote the attribute value. 您还应该引用属性值。 Full example: 完整示例:

$query = "SELECT date  FROM arrival order by date" ; 
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;  
echo "<select name='per1' id='per1'>";

while ($row = mysqli_fetch_array ($result)){?>
<option value='<?php echo $row['date'];?>' selected='selected'><?php echo $row['date'];?></option>
<?php } ?>
</select>

A little disorganized clear it up a bit and try this 有点混乱,请清理一下,然后尝试

$query = "SELECT date  FROM arrival order by date" ; 
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;

$result_array = array();

while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['date'];
}
echo "<select name='per1' id='per1'>";
foreach($result_array as $name){
echo'<option value="'.$name.'">'.$name.'</option>';
}
echo'</select>';

I believe that will do what you're looking for. 我相信这会满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM