2012-04-12 109 views
0

這是另一個。同一天兩次。我試圖設置4個選擇框,如果它們等於enSlot的值,則顯示基於Target1-4中的值選擇的選項。PHP沒有正確更新選擇框

echo "<select id='target".$row['paSlot']."' size='1' style='width:90px;'>"; 
$query = "SELECT * FROM enemy WHERE enChar = ".$_SESSION['chNum']." ORDER BY enSlot"; 
$result1 = mysql_query($query, $_SESSION['connect']) or die('Error 150: '.mysql_error()); 
while ($row1 = mysql_fetch_array($result1)) { 
    $s = ""; 
    if ($_GET['Target1'] == $row1['enSlot']) { 
      $s = "selected='selected'"; 
    } 
    if ($_GET['Target2'] == $row1['enSlot']) { 
      $s = "selected='selected'"; 
    } 
    if ($_GET['Target3'] == $row1['enSlot']) { 
      $s = "selected='selected'"; 
    } 
    if ($_GET['Target4'] == $row1['enSlot']) { 
      $s = "selected='selected'"; 
    } 
    echo "<option value=".$row1['enSlot']." ".$s.">".$row1['enSlot'].". ".$row1['enRace']; 
} 
echo "</select>"; 

我與此運行:

Target1時= 1,目標2 = 2,Target3 = 3,Target4 = 4 敵人有4個記錄,enSlot = 1 - 4

當執行我得到所有4個選擇框顯示被選中的Target4的最後一個值。

回答

1

的問題是在你的邏輯無論哪個條件評估值$老是會被選中=選擇...你可以試試這個:

while ($row1 = mysql_fetch_array($result1)) { 
    foreach($_GET[] as $target) { 
     if($target == $row1['enSlot']) { 
      echo "<option value=".$row1['enSlot']." selected=selected>".$row1['enSlot'].". ".$row1['enRace']."</option>"; 
     } else { 
      echo "<option value=".$row1['enSlot'].">".$row1['enSlot'].". ".$row1['enRace']."</option>"; 

     } 
    } 
} 
2
echo "<option value=".$row1['enSlot']." ".$s.">".$row1['enSlot'].". ".$row1['enRace']; 

我看到失蹤</option>

+0

謝謝,馬虎編程,但沒有解決問題。 – 2012-04-12 14:47:19

+0

$ s =「selected = selected」;總是設置? – 2012-04-12 14:48:50

+0

你的權利,現在要弄清楚如何使它工作 – 2012-04-12 14:56:33