
Originally Posted by
jezjsa
Hi.
What do i need to add to this code to make it match and select the value in the database? Usual coder off ill today so any help would be great...
<select name="subdivision" id="subdivision" class="contform">
<?php
$sql_d = "SELECT * FROM `sub_division` WHERE `division_id` = '".$row_enquiries['division']."'";
$res_d = mysql_query($sql_d, $dve) or die(mysql_error());;
while($row_d = mysql_fetch_assoc($res_d)) {
echo "<option value=\"".$row_d['sub_division_id']."\">".$row_d['name']."</option>";
}
?>
</select>
You need an if statement to figure out if the current value is the same as the value from the database. Also, noticed you are using "s and 's in a strange way. Best to use ' for php echo when you are producing HTML to save on \'s, it also looks neater and easiyer to read. See below for an example on how you can add the if statement (ternary):
PHP Code:
<select name="subdivision" id="subdivision" class="contform">
<?php
$sql_d = "SELECT * FROM `sub_division` WHERE `division_id` = '$row_enquiries['division']'";
$res_d = mysql_query($sql_d, $dve) or die(mysql_error());
while($row_d = mysql_fetch_assoc($res_d)) {
echo '<option value="' . $row_d['sub_division_id'] . '"';
echo ($somevalue == $row_d['sub_division_id']) ? ' selected="selected" ':'';
echo '>' . $row_d['name'] . '</option>';
}
?>
</select>
Bookmarks