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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了