简体   繁体   English

我有将信息从SQL Server下拉到下拉框中的PHP表单。 我希望将框中的特定项目作为默认选中

[英]I have PHP form that is pulling information into a dropdown box from SQL Server. I want a particular item in the box as default selected

This code is pulling from DB table, and need it to display Information & Tecnology SVCS - 41515 as the default: 这段代码是从数据库表中提取的,需要它来显示Information&Tecnology SVCS-41515作为默认值:

    echo '<option value=" ' . $row['department']. '"' . ($row['department'] == "INFORMATION & TECHNOLOGY SVCS -41515") ? ' selected="selected"' : "".'>'.$row['department']. '</option>';

This Code will give me the drop down but not a selected define item: echo ''.$row['department']. 该代码将为我提供下拉菜单,但没有给我提供选定的定义项:echo''。$ row ['department']。 ''; '';

Department: 部:

    <?php

    $sql = "SELECT department FROM Department";
    $query = sqlsrv_query($conn,$sql);
            $query_display = sqlsrv_query($conn,$sql);
    while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC))
    {
        //echo '<option value=" '. $row['department'].' ">'.$row['department']. '</option>';
        echo '<option value=" ' . $row['department']. '"' . ($row['department'] == "INFORMATION & TECHNOLOGY SVCS -41515") ? ' selected="selected"' : "".'>'.$row['department']. '</option>';
        continue;
    }
    ?>
    </select>
</td>

Put parentheses around the conditional expression, because operator precedence is not what you're expecting. 在条件表达式两边加上括号,因为运算符的优先级不是您所期望的。

    echo '<option value=" ' . $row['department']. '"' . (($row['department'] == "INFORMATION & TECHNOLOGY SVCS -41515") ? ' selected="selected"' : "").'>'.$row['department']. '</option>';

To make it easier to read, I usually use a separate variable: 为了便于阅读,我通常使用一个单独的变量:

if ($row['department'] == "INFORMATION & TECHNOLOGY SVCS -41515") {
    $selected = ' selected="selected"';
} else {
    $selected = "";
}
echo '<option value=" ' . $row['department']. '"' . $selected .'>'.$row['department']. '</option>';
($row['department'] == "INFORMATION & TECHNOLOGY SVCS -41515")

There was a spacing issue. 存在间距问题。 Needed to be 需要是

($row['department'] == "INFORMATION & TECHNOLOGY SVCS - 41515")

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

相关问题 我有PHP填充下拉列表框,并且我希望它也填充来自同一表的两个只读文本框 - I have PHP populating a Dropdown box, and I want it to also populate two read only Text boxs from the same Table PHP Dropdown从SQL数据库中提取信息 - PHP Dropdown pulling information from SQL database 有一个从SQL Server提取以呈现表数据的PHP表。 我想向列添加列标题过滤器以缩小范围 - Have a PHP table that is pulling from SQL Server to present table data. I want to add column header filters to the columns to narrow down 如何在下拉列表框中包含列表和插入值,作为PHP中UPDATE的默认值? - How can I have dropdown box with the list and inserted value as default for UPDATE In PHP.? 我希望根据是否使用php选中复选框来决定表单操作 - I want the form action to be decided based on whether a check box has been selected or not using php 我如何从组合框中选择项目到codeigniter中的控制器 - How i get selected item from combo box to controller in codeigniter 我有一个PHP表单下拉菜单,需要发送信息 - I have a php form dropdown menu that needs to send information PHP:下拉框中的选定值 - PHP: selected value in dropdown box 用PHP和数据库选择下拉框 - Dropdown box selected with php and database 我想将选定的选项从一个文本框传递到第二个框 - I want to pass a selected choice from one text box to a second box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM