我正在尝试从具有特定值的表中拉取一个链接,并从同一个表中拉取相同的链接但值不同。但是两个下拉菜单都出现在链接中。
<div class="col-sm-9">
<select style="width:350px;" name="featuredname" >
<?php
$selectunfeatured=mysql_query("select * from `properties` where `featured`='0' and `block`='0' and `active`='1' and `type`='1'");
while($selectunfeaturedarray=mysql_fetch_array($selectunfeatured))
{
echo '<option value='.$selectunfeaturedarray['property_id'].'>'.$selectunfeaturedarray['property_title'].'</option>';
}
?>
型号:
<select style="width:350px;" name="featuredname" >
<?php
$selectunfeatured=mysql_query("select * from `properties` where `featured`='0' and `block`='0' and `active`='1' and `type`='2'");
while($selectunfeaturedarray=mysql_fetch_array($selectunfeatured))
{
echo '<option value='.$selectunfeaturedarray['property_id'].'>'.$selectunfeaturedarray['property_title'].'</option>';
}
?>
</select>
最佳答案
当您在 <select>
中执行相同的操作时elements 我建议你创建一个函数来提取你需要的所有数据,方法是传递 type
值作为函数参数:
<?php
function getData($type){
$db=mysql_connect('database_host','database_user','database_pass');
mysql_select_db('database_name',$db);
$selectunfeatured = mysql_query("select * from `properties` where `featured`='0' and `block`='0' and `active`='1' and `type`='$type'");
while ($selectunfeaturedarray = mysql_fetch_assoc($selectunfeatured)) {
$data[] = $selectunfeaturedarray;
}
return $data;
}
?>
并生成<option>
元素的做如下:
<div class="col-sm-9">
<select style="width:350px;" name="featuredname[male]">
<?php foreach(getData(1) as $male):?>
<option value="<?=$male['property_id'];?>"><?=$male['property_title'];?></option>
<?php endforeach;?>
</select>
<select style="width:350px;" name="featuredname[female]">
<?php foreach(getData(2) as $female):?>
<option value="<?=$female['property_id'];?>"><?=$female['property_title'];?></option>
<?php endforeach;?>
</select>
</div>
注意:在您的示例中,select
元素具有相同的 name
属性。在这种情况下,仅选择第二个值 select
将被保存。如果你想得到它们,那么你必须把方括号 []
在每个name
的结尾属性值 name="featuredname[]"
或 name="featuredname[male]"
&& name="featuredname[female]"
关于php - 在 PHP 中下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837678/