0

I couldn't find a solution on here, thought I wasn't sure what to search for. I have a select box that is populated from my database using PDO. However I was unsure on how to set the selected value to a team that has already been set from the DB.

Update The code only shows the team that matches the variable, however I need to show all teams in the drop down.

<option <?php if($row4['team_id'] == $team_id) {  ?> 
 selected="selected" > <?php } 
 echo $row4['team_name'];?>
 </option>
 <?php  } ?>

OLD code

 $stmt4 = $conn ->prepare('SELECT team_id, team_name FROM team_tbl ORDER BY team_name');
 $stmt4->execute();
   while($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
echo '<option <? if ($row4['team_id'] == $team_id){?> selected="selected"<? } ?>>'.$row4['team_name'].'</option>';
   }
 ?>
  • possible duplicate of [How to set default value for HTML select element?](http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element) – CBroe Oct 08 '13 at 12:01
  • @CBroe i had checked that link before, that is for a hard coded list, I was looking for something dynamic. Thanks – Environment Oct 08 '13 at 12:48
  • And I was hoping that you could _transfer_ that by thinking about it a little bit yourself … – CBroe Oct 08 '13 at 12:52

1 Answers1

0
<option <?php if($row4['team_id'] == $team_id) { ?> selected="selected" <?php } ?>></option>

Add the above code to the option field.

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54