dongll0502 2012-09-12 10:34
浏览 51
已采纳

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>";
  • 写回答

3条回答 默认 最新

  • douwen9540 2012-09-12 10:43
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?