[英]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.