jeni
jeni

Reputation: 440

display from database to dropdown list

I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?

Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>

i have stored in database using value of selection but i dont know to display that value in same drop down

Upvotes: 1

Views: 14658

Answers (6)

shashikant verma
shashikant verma

Reputation: 1

<div class="form-group has-success col-md-6">
    <label class="control-label" for="state-success">Select Buyer</label>
 <select id="state-success" class="form-control ">
         <?php
   $sql="SELECT full_name FROM `new_customer`";
    $data=mysqli_query($dbcon,$sql);
    ?>
       <option>Select byer...</option>
   <?php while($row1=mysqli_fetch_array($data)){?>
       <option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
        <?php } ?> 
     </select>

Upvotes: -1

Addsy
Addsy

Reputation: 4054

Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after

Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>

This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.

If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above

Upvotes: 0

GordyD
GordyD

Reputation: 5103

Use something like this:

       <?
         $sql = "SELECT id, description FROM dropDownTable";
         $rs = mysql_query($sql);
       ?>
       <select name="dropDown">
         <option value="-1">Please select...</option>
       <? while ($obj = mysql_fetch_object($rs)) { ?>
         <option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
           <?= $obj->description; ?>
         </option>
       <? } ?>
       </select>

Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!

Upvotes: 1

rahularyansharma
rahularyansharma

Reputation: 10755

i used the COOKIE to match the value that should be selected when it comes to edit the form.

  <?php
    while($result_row=mysql_fetch_array($result)){
    if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
    {
    echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
    }
    else
    {
    echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
    }
    ?> 

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

Well you could do like this

   $query = "select id, label from lookup_table";
   $result = mysql_query($query);
   $html = "<select name='yourname'><option value="">Please select...</option>";
   while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $html .= "<option value='$row[id]'>$row[label]</option>";
   }
   $html = "</select>";
   echo ($html);//Display the select in the page

Upvotes: 1

Marek Sebera
Marek Sebera

Reputation: 40641

If you're able to get db data into array, you can work with them like this:

<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
    echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>

Upvotes: 0

Related Questions