2011-12-22 74 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