Boris Vellekoop
Boris Vellekoop

Reputation: 1

Dropdown keep it selected

I Like to get a list of battlenames in a dropdown, this i managed. But now i would like to keep one of them selected. But i can't find the right if statment to compare with my $rows.

Thanks a lot

< ?php
echo " < select name = 'battle' > ";

while (($rows = mysqli_fetch_array($result)) != null)

{
   echo "< option value = '{$rows['battle_number']}'";

   if ($result['battle_number'] == $rows['battle_number'])

    echo "selected = 'selected'";

    echo ">{$rows['name']}</option>";
}
echo "< /select>";

? >

Upvotes: 0

Views: 56

Answers (1)

Pupil
Pupil

Reputation: 23958

Corrected and simplified code:

<?php
echo '<select name="battle">';
while ($rows = mysqli_fetch_array($result)) {
  $selected = ($result['battle_number'] == $rows['battle_number']) ? 'selected="selected"' : '';
  echo '<option value = "'.$rows['battle_number'] . '" ' . $selected. '>' . $rows['name'] . '</option>';
}
echo '</select>';
?>

Upvotes: 1

Related Questions