0

when i try to selected option from sql in dropdown list i get all options is selected :

$status1 = $row7["status"];

if($status1 == "draft") { $slc = "selected"; }
if($status1 == "ordered") { $slc = "selected"; } 
if($status1 == "shipped") { $slc = "selected";  }

echo "
  <select class=\"form-control input-sm\" name = \"o_status\">
    <option value = \"draft\" $slc>draft</option>
    <option value = \"ordered\" $slc>ordered</option>
    <option value = \"shipped\" $slc>shipped</option>
  </select>
";

my problem with output selected all options like that :

  <select class="form-control input-sm" name = "status">
<option value = "draft" selected>draft</option>
<option value = "ordered" selected>shipped</option>
<option value = "shipped" selected>shipped</option>

0

3 Answers 3

4

Please check now.

Hope this helps you:

$status1 = $row7["status"];

if($status1 == "draft") { $draft = "selected"; }
if($status1 == "ordered") { $ordered = "selected"; } 
if($status1 == "shipped") { $shipped = "selected";  }

  echo "
  <select class=\"form-control input-sm\" name = \"o_status\">
 <option value = \"draft\" $draft>draft</option>
 <option value = \"ordered\" $ordered>ordered</option>
 <option value = \"shipped\" $shipped>shipped</option>
 </select>
  ";
Sign up to request clarification or add additional context in comments.

2 Comments

This will cause error like PHP Notice: Undefined variable: ordered in thisfile.php on line 22 cause when one is selected, other does not defined. Take a look on my reply ;)
@Sky working fine? Did you look at error.log ? :) This code have errors 100%, take a look ;)
4

You can define an array of statuses in order to set the select options:

<?php
    $statuses = array(
        'draft' => 'New draft',
        'ordered' => 'New order',
        'shipped' => 'New ship'
    );
?>

<select class="form-control input-sm" name="status">
<?php
    foreach ($statuses as $status => $name) {
?>
    <option value="<?php echo $status ?>" <?php echo $status === $row7['status'] ? 'selected="selected"' : '' ?>>
        <?php echo $name ?>
    </option>
<?php
    }
?>
</select>

2 Comments

if options name different options value how i can set it ?
Yes you can: try this: <?php $statuses = array('draft'=>'something1', 'ordered'=>'something2', 'shipped'=>'something3'); $row7['status'] = 'draft'; ?> <select class="form-control input-sm" name="status"> <?php foreach ($statuses as $status=>$value) { ?> <option value="<?php echo $value ?>" <?php echo $status === $row7['status'] ? 'selected="selected"' : '' ?>> <?php echo $status ?> </option> <?php } ?> </select>
1

Maybe this helps:

<?php
switch ($row7["status"]) {
    case 'draft':
        $draft = "selected";
        $shipped = "";
        $ordered = "";
        break;
    case 'ordered':
        $ordered = "selected";
        $draft = "";
        $shipped = "";
        break;
    case 'shipped':
        $shipped = "selected";
        $draft = "";
        $ordered = "";
        break;
    default:
        $draft = "";
        $shipped = "";
        $ordered = "";
        break;
}
echo "<select class=\"form-control input-sm\" name = \"o_status\">
    <option value = \"draft\" $draft>draft</option>
    <option value = \"ordered\" $ordered>ordered</option>
    <option value = \"shipped\" $shipped>shipped</option>
  </select>";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.