PHP <select> <option> showing selected <option> twice

Viewed 70

I have a problem where the <option selected> showing twice.
I am populating the <select><option> with data from titles table
I want to show selected option based on data from taos table.

The data output is shown as: https://i.stack.imgur.com/P7V4E.png and https://i.stack.imgur.com/nBFPh.png

<?php

$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";

 if ($result = mysqli_query($conn, $title_sql)) {
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

   $dbselected = $tao['title_id'];

    foreach ($row as $option) {
     if ($dbselected == $option) {
?>
     <option selected value='$row[id]'><?php echo $row['title']; ?></option>
<?php
     } else {

     }
   }
   echo "<option value='$row[id]'>$row[title]</option>";
  }
  mysqli_free_result($result);
}
echo "</select>"; ?>

<?php

    $title_sql = "SELECT * FROM titles";
    echo "<select class='form-control' name='title_id'>";
    
    if ($result = mysqli_query($conn, $title_sql)) {
      while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    
       $dbselected = $tao['title_id'];
    
        foreach ($row as $option) {
           if ($dbselected == $option) {    
?>
             <option selected value='$row[id]'>
                 <?php echo $row['title']; ?>
             </option>
           <?php
           } else {
    
           }
        }
           echo "<option value='$row[id]'>$row[title]</option>";
      }
      mysqli_free_result($result);
    }
    echo "</select>"; ?>



    

Any assistance would be appreciated.

4 Answers

In the foreach section you left else section empty, and show your all select values outside the condition

try this

<?php

$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";

 if ($result = mysqli_query($conn, $title_sql)) {
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

   $dbselected = $tao['title_id'];

    foreach ($row as $option) {
     if ($dbselected == $option) {
?>
     <option selected value='$row[id]'><?php echo $row['title']; ?></option>
<?php
     } else {
       echo "<option value='$row[id]'>$row[title]</option>";
     }
   }
  }
  mysqli_free_result($result);
}
echo "</select>"; ?>

Your if statement is outputting the option with selected when it is equal to the $dbselected value. Then you are outputting the option (without selected) unconditionally. Move the last echo into the else { portion of that if to prevent it from being output if you have already outputted with selected

Also, you do not want the foreach ($row as $option) {; as that is walking through the columns of the row, so the second echo will be repeated for each column.

Also, be consistent. Either use echo or drop out of PHP; don't do it one way and then the other.

Try it this way:

 if ($result = mysqli_query($conn, $title_sql)) {
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

   $dbselected = $tao['title_id'];

   ## foreach ($row as $option) { ## TAKE THIS OUT
     if ($dbselected == $option) {
?>
       echo "<option selected value='{$row['id']}'>{$row['title']}</option>";
<?php
     } else {
       echo "<option value='{$row['id']}'>{$row['title']}</option>"; ## MOVED FROM BELOW
     }
   ## } ## (End of foreach) TAKE THIS OUT 
   ## echo "<option value='$row[id]'>$row[title]</option>"; ## MOVED THIS UP
  }

I would actually use printf() instead of the if-else:

printf('<OPTION %s value="%s">%s</OPTION>',
    ($dbselected == $row['id'] ? "selected" : ""),
    $row['id'], 
    htmlspecialchars($row['title']));

I edited your question to show you how better formatting makes it easier to find your problem. your third echo statement is being executed first, It is outside your else block and should be inside it

<?php

    $title_sql = "SELECT * FROM titles";
    echo "<select class='form-control' name='title_id'>";
    
    if ($result = mysqli_query($conn, $title_sql)) {
      while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    
       $dbselected = $tao['title_id'];
    
        foreach ($row as $option) {
           if ($dbselected == $option) {    
?>
             <option selected value='$row[id]'>
                 <?php echo $row['title']; ?>
             </option>
           <?php
           } else {
    
           }
        }
           echo "<option value='$row[id]'>$row[title]</option>";
      }
      mysqli_free_result($result);
    }
    echo "</select>"; ?>

Look no further <select><option> from database showing selected option if found in another table Select an option

<?php
$sql = "SELECT * FROM titles";
$result = mysqli_query($conn, $sql);
$selected = $resident['title_id'];
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
?>

 <?php foreach ($rows as $row) : ?>
  <?php if ($selected == $row['id']) { ?>
   <option selected value="<?php echo $row['id']; ?>"> 
   <?php echo $row['status']; ?></option>
   <?php } else { ?>
   <option value="<?php echo $row['id']; ?>">
  <?php } ?>
 <?php echo $row['status']; ?></option>
 <?php endforeach; ?>
</select>
Related