在下拉列表中列出枚举值 php mysql

发布于 2024-10-03 10:10:34 字数 147 浏览 2 评论 0原文

我有一个 mysql 表,其中包含以下列。

 Id     Name      Sex

性别列的类型为枚举('男性','女性','未指定')

我如何在下拉列表中列出枚举值并使当前存储的值成为选定的

I have a mysql table which contains following cols.

 Id     Name      Sex

and sex column have type of enum('Male','Female','Unspecified')

How can i list enum values in a dropdown and make current stored value as selected

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2024-10-10 10:10:34

检查此链接...它非常棒..该脚本可重复用于任何枚举列:

http://jadendreamer.wordpress.com/2011/03/16/php-tutorial-put-mysql-enum-values-into-drop-down-select -框/

Check this link...its pretty awesome..the script is reusable for any enum column:

http://jadendreamer.wordpress.com/2011/03/16/php-tutorial-put-mysql-enum-values-into-drop-down-select-box/

深巷少女 2024-10-10 10:10:34

在创建选择(下拉)字段时,它是一个枚举字段这一事实并不重要。枚举字段的行为与文本输入字段相同,它们只是拒绝任何与枚举选项不匹配的数据并更有效地存储数据。因此,与枚举字段交互与与文本输入字段交互相同。

所以你将需要一个普通的 html 选择字段:

<form>
  <select name="gender">
    <option value="Unspecified">Unspecified</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option
  </select>
</form>

并且你将需要选择你的值:

<form>
  <select name="gender">
    <option value="Unspecified" <?php if($gender == "Unspecified") { echo "SELECTED"; } ?>>Unspecified</option>
    <option value="Male" <?php if($gender == "Male") { echo "SELECTED"; } ?>>Male</option>
    <option value="Female" <?php if($gender == "Female") { echo "SELECTED"; } ?>>Female</option
  </select>
</form>

这可以分解为函数:

function gender_select($default_value='') {
  $select = '<select name="gender">';
  $options = array('Unspecified','Male','Female',);
  foreach($options as $option) {
    $select .= write_option($option, $option, $default_value);
  }
  $select .= '</select>';
  return $select;  
}

function write_option($value, $display, $default_value='') {
  $option = '<option value="'.$value.'"';
  $option .= ($default_value == $value) ? ' SELECTED' : '';
  $option .= '>'.$display.'</option>';
  return $option;
}

所以你的最终代码将是:

<form>
<?php echo $gender_select($gender); ?>
</form>

The fact that it is an enum field doesn't matter much when creating the select (dropdown) fields. Enum fields behave the same as a text input field, they just reject any data that doesn't match an enum option and store the data more efficiently. Thus, interacting with an enum field is the same as interacting with a text input field.

So you will need a normal html select field:

<form>
  <select name="gender">
    <option value="Unspecified">Unspecified</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option
  </select>
</form>

And you will need to select your value:

<form>
  <select name="gender">
    <option value="Unspecified" <?php if($gender == "Unspecified") { echo "SELECTED"; } ?>>Unspecified</option>
    <option value="Male" <?php if($gender == "Male") { echo "SELECTED"; } ?>>Male</option>
    <option value="Female" <?php if($gender == "Female") { echo "SELECTED"; } ?>>Female</option
  </select>
</form>

This can be broken out into functions:

function gender_select($default_value='') {
  $select = '<select name="gender">';
  $options = array('Unspecified','Male','Female',);
  foreach($options as $option) {
    $select .= write_option($option, $option, $default_value);
  }
  $select .= '</select>';
  return $select;  
}

function write_option($value, $display, $default_value='') {
  $option = '<option value="'.$value.'"';
  $option .= ($default_value == $value) ? ' SELECTED' : '';
  $option .= '>'.$display.'</option>';
  return $option;
}

So your final code would be:

<form>
<?php echo $gender_select($gender); ?>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文