doulilou8560 2013-09-08 08:00
浏览 19
已采纳

尝试自动选择下拉列表时出现PHP数组问题

Note: $i is set earlier in the code and there are 25 drop down boxes, the array shows the first 5 have values, so I need to set those drop downs to selected and show the relevant value.

I have an array that contains the following:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => )

$i=1;
for ($p=1; $p<=25; $p++){
  for ($pp=1; $pp<=5; $pp++){
   echo "<select id='rqa".$i."".$p."' class='business' name='q".$i."[]' onclick='mand();'> 
    <option value='' selected='selected'>".$rank."</option>
    <option value='".$pp."'"; 
        if (in_array($p, $arr)) echo 'selected';
    echo ">".$pp."</option>";
  }
}

All I am trying to do is set set the relevant dropdown as selected and also its value. Make sense?

All this is doing is setting the first 5 dropdowns to selected but their value is 5, instead on 1,2,3,4,5.

The problem I have having

  • 写回答

3条回答 默认 最新

  • douying3251 2013-09-08 10:33
    关注

    Try the following code...

    $arr = array (1,2,3,4,5,'','','','','','','','','','','','','','','','','','','','');
    $i=1;
    for ($p=1; $p<=25; $p++){
        echo "<br><select id='rqa".$i."".$p."' class='business' name='q".$i."[]' onclick='mand();'>";    
        for ($pp=1; $pp<=5; $pp++){
            $selected = "";
            if ($pp == $arr[$p-1]){
                $selected = "selected='selected'";
            }
            echo "<option value='$pp' ". $selected .">".$pp."</option>";
        }
        echo "</select>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了