2011-12-22 72 views
2

我需要保持选择用户在多选菜单中选择的选项。这是迄今为止的代码,但仍然不起作用。感谢您的帮助!保持选择多个选项中的选项PHP

<select name="cb_estatura2[]" size="6" multiple="multiple" class="inputbox" id="cb_estatura2"> 
<?php 
$height = array("57","58","59","60"); 
$choosen_height = $_GET['cb_estatura2']; 

for ($i=0;$i<count($height);$i++) 
    { 
     $selected = ($height[$i] == $choosen_height[$i] ? 'selected="selected"' : ''); 
     echo "<option value='$height[$i]' $selected>$height[$i]</option>"; 
    } 
?> 
</select> 
+0

您将需要使用in_array来检查密钥($ height [$ i])是否在get变量中。 – Corbin 2011-12-22 04:40:08

+0

我只是猜测,但我不认为你需要在'$ chosen_height'上的'[$ i]''。我很难告诉出更多的信息,但我猜测这只是一个整数而不是数组。 – mikelbring 2011-12-22 04:40:13

+0

运行'print_r($ _ GET ['cb_estatura2']);'并发布它说的内容。 – Blender 2011-12-22 04:40:27

回答

7

一两件事,应该工作会是这样:

<select name="cb_estatura2[]" size="6" multiple="multiple" class="inputbox" id="cb_estatura2"> 
<?php 
$height = array("57","58","59","60"); 
$choosen_height = $_GET['cb_estatura2']; 

for ($i=0;$i<count($height);$i++) 
    { 
     $selected = (in_array($height[$i],$choosen_height) ? 'selected="selected"' : ''); 
     echo "<option value='$height[$i]' $selected>$height[$i]</option>"; 
    } 
?> 
</select> 

这应该工作,只是一个事实,即$_GET['cb_estatura2']不填充像$height阵列。可能是错的,没有测试过。

+1

谢谢!这工作完美,你救了我的生活蒂姆:) – typologist 2011-12-22 04:52:32

+0

不是问题:) – 2011-12-22 04:56:10