Reputation: 75
Here is another one. Twice in the same day. I'm trying to set up 4 select boxes that shows a option selected based the values in Target1-4 if they are equal to the value of enSlot.
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>";
I run it with this:
Target1 = 1, Target2 = 2, Target3 = 3, Target4 = 4 Enemy has 4 records, enSlot = 1 - 4
When it executes I get all 4 select boxes show the last value of Target4 being selected.
Upvotes: 0
Views: 66
Reputation: 4250
The problem is in your logic no matter which condition evaluates the value of $s always will be selected=selected... You may try this:
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>";
}
}
}
Upvotes: 1
Reputation: 15080
echo "<option value=".$row1['enSlot']." ".$s.">".$row1['enSlot'].". ".$row1['enRace'];
i see missing </option>
Upvotes: 2