簡體   English   中英

使用PHP填寫下拉選擇的值

[英]filling in value of drop down selection using php

我有一個下拉菜單,用戶可以從中選擇一個位置。 這些是保存的設置,因此,如果用戶要編輯這些設置,我希望以前保存的值可以填充列表。

該位置及其ID是從名為locations的表中選擇的,但是,只有該位置保存在名為userinfo的表中。 考慮到第一次在選擇過程中使用了ID,我不確定如何再次填充選擇列表。

這就是我現在要選擇的乳液。 我想為此填寫以前保存的位置。 這是較大表格的一部分,因此沒有表格標簽。

<?
$sql = mysql_query("select locationsid,locationsName from locations"); 
$selection="";
while ($row=mysql_fetch_array($sql)){ 
   $id=$row["locationsid"]; 
   $locationName = $row["locationsName"]; 
   $selection.="<OPTION VALUE=\"$id\">".$locationName; 
}
?>
<!--select location - used for reach location -->
<h4>Nearest Location</h4>
<table>
<tr><td>Location:</td><td><select name ="location"><Option value="<? echo selection2; ?>">Choose Location<? echo $selection; ?></Select></td>
</table>

我假設我必須查詢數據庫以獲取保存的位置(在userinfo表中),然后從locations表中查找該位置的ID。 從這里開始,我不太確定如何將其填寫到值選擇字段中。

$locationLookup = mysql_query("select location from userinfo where userid = $userid");
$locationLookup = mysql_fetch_array($locationLookup);
$location = $locationLookup['location'];
$idlookup = mysql_query("select locationsID from locations where locationsName = '$location'");
$idlookup = mysql_fetch_array($locationid);
$idlookup = $locationid['locationsID'];

因此,這將為我提供名稱和ID,如何將其設置為選擇列表中的值?

得到$idlookup

$idlookup = $locationid['locationsID'];

while循環中使用它:

while ($row=mysql_fetch_array($sql)){ 
   $id=$row["locationsid"]; 
   $locationName = $row["locationsName"]; 
   $selected = ($id == $idlookup) ? 'selected="selected"' : '';
   $selection.="<OPTION " . $selected . " VALUE=\"$id\">".$locationName."</OPTION>";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM