如何在下拉列表中回显先前选择的选项?

如何在下拉列表中回显先前选择的选项?

问题描述:

I am using the follow bit below. I am first querying the db to see all available options but this is for an edit form so i want the drop down to show the previously selected value so i did another query and got the selection. When i do it the way i have it below it will keep repeating the previously selected selection after each available option. HOw to fix this?

<option>Select Sales rep</option>
<?php

$query="select agent_id, agent_name from agent_names where agent_id='$ad' 
order by agent_name asc";
$result=mysql_query($query);
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
    echo "<option value=\"".$previousname."\">".$previousselection."</option>
    <option value=\"".$agent_id."\">".$agent_name."</option>";
}


?>

我使用下面的跟随位。 我首先查询数据库以查看所有可用选项,但这是一个编辑表单,所以我希望下拉显示以前选择的值,所以我做了另一个查询并得到了选择。 当我按照下面的方式进行操作时,它会在每个可用选项后继续重复之前选择的选项。 要解决此问题吗? p>

 &lt; option&gt;选择销售代表&lt; /选项&gt; 
&lt;?php 
 
 $ query =“select agent_id,agent_name from agent_names where  agent_id ='$ ad'
order by agent_name asc“; 
 $ result = mysql_query($ query); 
while(list($ agent_id,$ agent_name)= mysql_fetch_row($ result)){
 echo”&lt; option  value = \“”。$ previousname。“\”&gt;“。$ previousselection。”&lt; / option&gt; 
&lt; option value = \“”。$ agent_id。“\”&gt;“。$ agent_name。”  &lt; / option&gt;“; 
} 
 
 
?&gt; 
  code>  pre> 
 
 

p> div>

Just check if $agent_id equals $previousname (perhaps you mean $previousid?) and echo selected="selected" if so:

while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
    $selected = $agent_id == $previousname;
    echo "<option " . ($selected ? "selected=\"selected\"" : "") . " value=\"".$agent_id."\">".$agent_name."</option>";
}

Another option is to output the previous selected item before your while loop, and exclude it in your sql query.

you should take the previos selection out of the while.. like this:

<?php

$query="select agent_id, agent_name from agent_names where agent_id='$ad' 
order by agent_name asc";
$result=mysql_query($query);
echo "<option value=\"".$previousname."\">".$previousselection."</option>";
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
    echo "<option value=\"".$agent_id."\">".$agent_name."</option>";
}


?>

and maybe even add an if so it wont repeat it self:

$query="select agent_id, agent_name from agent_names where agent_id='$ad' 
order by agent_name asc";
$result=mysql_query($query);
echo "<option value=\"".$previousname."\">".$previousselection."</option>";
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
  if ($agent_id != $previousname) {
    echo "<option value=\"".$agent_id."\">".$agent_name."</option>";
  }
}

?>

You can force MySQL to return your desired result first in the list like this:

$query = "SELECT agent_id, agent_name FROM agent_names WHERE agent_id='$ad' 
ORDER BY agent_id = '{$previousname}' DESC, agent_name ASC";

This tells MySQL first to sort by agent_id matching previous selection (i.e. it will be 1 for the previously selected record and 0 for all others, hence sorting by it DESC makes it first in the list. And since all others will have this fields equal to 0, they will be sorted by second field, which is agent_name ASC