PHP从数据库中删除所有记录+选择当前设置记录

I have a PHP dropdown of a list of groupnames (together with id, so it can be updated). In this FORM page you can change the groupname specified for an item by choosing possibilities from the dropdown coming out from the database. My code below works, but there must be a better way, because I get the first field as the currently set, and then all the possibilities, so I get this record twice.

Example:
- Keyboard (Currently set)
- Speakers (Possible to choose, straight from DBS)
- Midi Controllers (Possible to choose, straight from DBS)
- Keyboard (Possible to choose, straight from DBS)
- Drum set (Possible to choose, straight from DBS)

As you see I get the currently set record again.

My code:

echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid 
FROM item, itemgroup 
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
    echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";

There are 2 ways to achieve what you're looking for:

1) To show the selected item at the top of the dropdown

echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid 
FROM item, itemgroup 
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup WHERE item.itemid != $itemid";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
    echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";

2) Show the selected item in it natural place

echo "<select name='itemgroupid'>";
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
    echo "<option value=$nt[itemgroupid]";
    if( $itemid == $nt['itemgroupid'] ) echo ' selected="selected"';
    echo ">$nt[itemgroupname]</option>";
}
echo "</select>";

HTH

OK

In your code. rather than output your selected value at the top, do it the proper way :)

Select your current item (make a note of the itemgroupid for example)

then in your output

while($nt=mysql_fetch_array($itemgroupqueryresult)){ 
    echo "<option ";
    if ($savedid==$nt[itemgroupid]) echo "selected ";
    echo "$nt[itemgroupid]>$nt[itemgroupname]</option>"; 
} 
echo "</select>"; 

This will produce with $savedid=1

<option value=0>group 0</option>
<option selected value=1>group 1</option>
<option value=2>group 2</option>

Add the default selected record to a empty array first like

toDisplay = array('selected_record');

Then, get the data from the database using your sql and append it to this array.

Later run a array_unique on it and finally using a loop create the html output string, in the same way you are doing it now.