SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    make an Option field selected from value in db

    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>

  2. #2
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jezjsa View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •