I am calling from my SQL server a list for the options and outputting into an array.. see query:
SQL
$location = "SELECT location_id, location_name, location_postcode, location_buildingname
FROM dbo.system_locations";
$stmt = sqlsrv_query( $conn, $location );
PHP
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option>
<?php echo $row['location_name']?>
</option>
<?php } ?>
</select>
This works great, however I want the option which is selected within the database to be the selected option..
There is one factor, the select list can be added to by visiting settings section and adding new location otherwise I would use the ternary operator method.
try this
<select class="form-control" name="location">
<?php while ($row = sqlsrv_fetch_array($stmt)){ ?>
<option <?php ($row['selected'] == 1){echo 'selected';}?>>
<?php echo $row['location_name']?>
</option>
<?php }?>
</select>