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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化