Edit Drop down box values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Edit Drop down box values

    I want to edit database values using my web application. So first i load data to test boxex and drop down list. But drop down list only contain one value. I want to put all posible value to that and should high light particular one. (Because if user want to edit drop down box value)How can i do that?
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    You will need to fill the drop down list with all the values, and then print selected in the drop down list if the value in the DB matches the one in the Drop Down list.


    Code:
    <?php
    $valueFromDB = "apples";
    ?>
    <select>
    <?php
    $arr = array("please select", "apples", "pears", "grapes");
    foreach ($arr as $value) {
        $selected= $value == $valueFromDB ? "selected" : "";
    ?>
    <option <?php echo $selected; ?>><?php echo $value; ?></option> 
    <?php
    }
    ?> 
    </select>

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      It is working ! Thanks

      Comment

      • harshmaul
        Recognized Expert Contributor
        • Jul 2007
        • 490

        #4
        No worries. I'm here to help!

        Comment

        • mcee

          #5
          i tried ur code above.. it works for me to insert but it didn't retrieve the exact info from database.. what i mean is it only show the last dropdown value only

          below is my code

          <?php
          $valueFromDB = "E1";
          $valueFromDB = "E2";
          $valueFromDB = "TM";
          $valueFromDB = "TP";
          ?>
          <select name="positiont ype">
          <?php
          $arr = array("Select One", "E1","E2", "TM", "TP");
          foreach ($arr as $row_rsXDsecond ary['positiontype']){
          // $row_rsXDsecond ary['positiontype'] = $value;
          $selected= $row_rsXDsecond ary['positiontype'] == $valueFromDB ? "selected" : "";
          ?>
          <option <?php echo $selected; ?>><?php echo $row_rsXDsecond ary['positiontype']; ?></option>

          }
          <?php
          }
          ?>
          </select>
          and it always show TP

          thank you in advance

          Comment

          Working...