php&选择mysql下拉选项问题

时间:2017-04-04 10:54:54

标签: php mysql dropdown

当我尝试从下拉列表中的sql中选择选项时,我选择了所有选项:

$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>
";

我的输出问题选择了所有选项:

  <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>

3 个答案:

答案 0 :(得分:4)

您可以定义状态数组以设置选择选项:

<?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>

答案 1 :(得分:4)

请立即查看。

希望这可以帮助你:

$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>
  ";

答案 2 :(得分:1)

也许这会有所帮助:

<?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>";
?>