php - 在 PHP 中下拉

标签 php jquery html css

我正在尝试从具有特定值的表中拉取一个链接,并从同一个表中拉取相同的链接但值不同。但是两个下拉菜单都出现在链接中。

enter image description here

<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/

相关文章:

PHP 到 Codeigniter 语法 - 从 db/form 输入中选择减法

javascript - 如果我想知道用户何时从输入中删除文本,即使用户不使用键盘,我应该跟踪什么事件

html - 我可以自动将 CSS 列分解为 'pages' 以垂直放置在视口(viewport)内吗?

javascript - 单击按钮时转到 Accordion 选项卡 Vanilla JavaScript

jquery - 使用 jQuery 跟踪 Flash 影片(对象/嵌入)上的点击

html - float 变化

PHP 文件输出 javascript 以包含为 JS 文件 - 需要限制,使其不会在浏览器窗口中打开

php - 在 foreach() 进程中查找最后一个条目

php - 将 2 个 sql 值添加到数组中

javascript - 将一个字符放入具有指定类别的空 div-s 中