17

I need to get selected value from db into select box. please, tell me how to do it. Here is the code. Note: 'options' value depends on the category.

<?php 
  $sql = "select * from mine where username = '$user' ";
  $res = mysql_query($sql);
  while($list = mysql_fetch_assoc($res)){
    $category = $list['category'];
    $username = $list['username'];
    $options = $list['options'];
?>

<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
   <option value="0">Please Select Option</option>
   <option value="PHP">PHP</option>
   <option value="ASP">ASP</option>
</select>

<?php 
  }
?>
Tempo
  • 305
  • 2
  • 11
Php Gemini
  • 209
  • 1
  • 4
  • 10

15 Answers15

41

I think you are looking for below code changes:

<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>
rakeshjain
  • 1,791
  • 11
  • 11
9

The easiest way I can think of is the following:

<?php

$selection = array('PHP', 'ASP');
echo '<select>
      <option value="0">Please Select Option</option>';

foreach ($selection as $selection) {
    $selected = ($options == $selection) ? "selected" : "";
    echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
}

echo '</select>';

The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.

Dharman
  • 30,962
  • 25
  • 85
  • 135
independent.guru
  • 580
  • 5
  • 18
5

for example ..and please use mysqli() next time because mysql() is deprecated.

<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'"; 
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>

<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>
chirag ode
  • 950
  • 7
  • 15
2

Select value from drop down.

<select class="form-control" name="category" id="sel1">
    <?php foreach($data as $key =>$value) { ?>                         
        <option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
    <?php } ?>
</select> 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Siddharth Shukla
  • 1,063
  • 15
  • 14
2

Just Add an extra hidden option and print selected value from database

<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
Nasser Hajlawi
  • 313
  • 2
  • 15
1

THE EASIEST SOLUTION

It will add an extra in your options but your problem will be solved.

<?php 
    if ($editing == Yes) {
        echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
    }
?>
Community
  • 1
  • 1
K. Raza
  • 11
  • 1
1
$option = $result['semester'];
<option >Select</option>
                    <option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
                    <option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
                    <option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
                    <option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
                    <option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
                    <option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
                    <option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
                    <option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
                </select>
0

BEST code and simple

<select id="example-getting-started" multiple="multiple" name="category">

    <?php
    $query = "select * from mine";
    $results = mysql_query($query);

    while ($rows = mysql_fetch_assoc(@$results)){ 
    ?>
    <option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>

    <?php
    } 
    ?>
</select>
Tempo
  • 305
  • 2
  • 11
razvan
  • 61
  • 11
0

You can also do like this ....

<?php  $countryname = $all_meta_for_user['country']; ?>

<select id="mycountry"  name="country" class="user">

    <?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
    foreach($myrows as $rows){
        if( $countryname == $rows->id ){ 
            echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
        } else{ 
            echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
        }
    }
    ?>
</select>
Draken
  • 3,134
  • 13
  • 34
  • 54
0

USING PDO

<?php
$username = "root";
$password = "";
$db = "db_name";

$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$conn = new PDO($dns,$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "select * from mine where username = ? ";

$stmt1 = $conn->prepare($sql);
$stmt1->execute(array($_POST['user']));
$all = $stmt1->fetchAll(); ?>

<div class="controls">
    <select  data-rel="chosen"  name="degree_id" id="selectError">
        <?php 
        foreach($all as $nt) { 
            echo "<option value =$nt[id]>$nt[name]</option>";
        }
        ?>
    </select>
</div>
Dharman
  • 30,962
  • 25
  • 85
  • 135
7ani9
  • 1
  • 1
0

Answer is simple. when u pass value from dropdown.

Just use as if else.

for eg:

foreach($result as $row) {                      
   $GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'. 
   ($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').' 
   >'.$row['valueetc'].'</option>';
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
siraj
  • 1
  • 3
0
<?php 
  $sql = "select * from mine where username = '$user' ";
  $res = mysql_query($sql);
  while($list = mysql_fetch_assoc($res)){
    $category = $list['category'];
    $username = $list['username'];
    $options = $list['options'];
?>

<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>

<?php 
  }
?>
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Mar 22 '21 at 09:16
0

I'm using eval() PHP function like this:

My PHP code:

$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');

Then in my select box I use it like this:

<select>
   <option <?=$selOps1?> value="1">big</option>
   <option <?=$selOps2?> value="2">Middle</option>
   <option <?=$selOps3?> value="3">Small</option>
</select>
cheinan
  • 79
  • 2
  • 7
0

Put value from db into a variable and check like following code example

<select class="form-control" name="currency_selling" required >
     <option value="">Select Currency</option>
     <option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
  <option value="dollar"  <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
  <option value="pounds"  <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
  <option value="dirham"  <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
  </select>
Hassan Qasim
  • 463
  • 5
  • 5
-1

This may help you.

?php 
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string 
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
   <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>

<?php 
}
?>
Pradeeshnarayan
  • 1,235
  • 10
  • 21