<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'">'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
This is Edit.php
This is the result of that code but i want is. after getting the data from the database. it will automatically selected the value of the option base on the database data.
Suppose the query result returned from tblchurchs
contains a field Selected
which is either set to 1
or 0
. If it is set to 1 then you have to select that option in your drop-down list, and if it is set to 0, it is not selected.
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'"';
if($get_detail['Selected'] == 1)
{
echo ' selected="selected"';
}
echo '>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
to getting selected for selected option. first you have to save the selected data getting from database to a variable and then using an if loop you can do this.
<?php
$AChurchID=1; // this is your already selected value that is in db .
?>
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
$sel="";
if($AChurchID==$get_detail['AChurchID']){
$sel="selected";
}
echo'<option value="'.$get_detail['AChurchID'].'" '.$sel.'>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
This will work in your case. This is works fine in ma case. Please have a try.