简体   繁体   English

PHP:在下拉列表中检索所有可能的选项,并为特定记录选择选项

[英]PHP: Retrieve all possible options within a dropdown and select option for specific record

I am still really new to PHP, I am creating a page for updating a specific record. 我对PHP还是很陌生,我正在创建一个页面来更新特定记录。 I have passed an ID into the URL of the page, and using the ID for getting specific data for that record. 我已将一个ID传递到页面的URL中,并使用该ID获取该记录的特定数据。

The trouble I am having is with dropdown lists. 我遇到的麻烦是下拉列表。 I want to pull back all possible options for that dropdown (which comes from a different table) and I have this sorted. 我想拉出该下拉菜单的所有可能选项(来自不同的表),并进行了排序。

$status_sql = "SELECT DISTINCT AssetStatus.AssetStatusTitle AS HardwareAssetAssetStatusTitle, HardwareAssetAssetStatusID FROM HardwareAsset INNER JOIN AssetStatus ON (AssetStatus.AssetStatusID = HardwareAsset.HardwareAssetAssetStatusID) ORDER BY HardwareAssetAssetStatusTitle ASC";
$status = sqlsrv_query($database_connection, $status_sql);
while($status_option = sqlsrv_fetch_object($status)){
    echo "<option value='$status_option->HardwareAssetAssetStatusID'>".$status_option->HardwareAssetAssetStatusTitle."</option>";
}

The issue comes when i want to have an option already selected for that record. 当我想为该记录选择一个选项时,就会出现问题。 I know to get the specific option i need the following: 我知道要获得特定的选项,我需要以下几点:

$status_sql = "SELECT DISTINCT AssetStatus.AssetStatusTitle AS HardwareAssetAssetStatusTitle, HardwareAssetAssetStatusID FROM HardwareAsset INNER JOIN AssetStatus ON (AssetStatus.AssetStatusID = HardwareAsset.HardwareAssetAssetStatusID)  WHERE HardwareAssetID = '".$HardwareAssetID."'";

but how do i combine these both together? 但是如何将两者结合在一起? Having the selected option, selected on load by default, and all other options available when opening the dropdown. 具有选定的选项,默认情况下在加载时处于选中状态,打开下拉列表时所有其他选项均可用。

I have spent a fair amount of time searching online, but I dont think I am searching for the right thing, as I havent really turned anything up. 我花了大量时间在网上搜索,但我认为我没有在寻找正确的东西,因为我还没有真正找到任何东西。 Any help appreciated. 任何帮助表示赞赏。

You don't have to modify the original SELECT query and put WHERE clause in it. 您不必修改原始的SELECT查询并将WHERE子句放入其中。 Simply change your while loop in the following way, 只需按照以下方式更改while循环,

while($status_option = sqlsrv_fetch_object($status)){
    $output = "<option value='$status_option->HardwareAssetAssetStatusID'";
    if($status_option->HardwareAssetAssetStatusID == $HardwareAssetID){
        $output .= " selected='selected'";
    }
    $output .= ">".$status_option->HardwareAssetAssetStatusTitle."</option>";
    echo $output;
}

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

相关问题 PHP选择下拉选项,通过mysql将特定选项放在顶部 - PHP select dropdown options putting specific options at top via mysql PHP:使用kartik Select2根据第一个下拉选择选项设置第二个下拉选项 - PHP: Set second dropdown options based on first dropdown choosen option using kartik Select2 PHP从数据库中删除所有记录,并选择当前设置的记录 - PHP Dropdown of all records from database + select current set record 在PHP中选择选项 - Select Option within PHP 如何使用php中的select选项从数据库检索所有数据? - How to retrieve all data from database using select option in php? PHP Dropdown请选择选项 - PHP Dropdown please select option php从下拉菜单中选择选项 - php select option from dropdown 将txt文件行传递到html文件POST表单选择选项,并将检索到的选项选择到php文件 - Pass txt file lines to html file POST form select options, and retrieve option selected to a php file 从 mysql db 创建了一个 Select 下拉选项,但我还想从同一表单下拉列表 PHP 添加一个 select 所有选项 - Created a Select dropdown option from mysql db but I also want to add a select all option from the same form dropdown PHP 从下拉列表中选择选项,取决于两个下拉列表,这将显示mysql数据库php的主题选项 - Select option from dropdown list depending on two dropdown list which will show subject options from mysql database php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM