HTML< Select> < Option>默认基于MySQL数据 [英] HTML <Select> <Option> default based on MySQL data

查看:98
本文介绍了HTML< Select> < Option>默认基于MySQL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的库存数据库,在input_data表单上,输入字段之一是field,如下所示:

I have a simple inventory database and on the input_data form, one of the input fields is field, as follow:

<select>
    <option>In Inventory</option>
    <option>In Process</option>
    <option>Shipped/in-transit</option>
    <option>Received by recipient</option>
</select>

例如,如果我使用处理中"输入数据,后来又想更新该垂直数据.默认返回我的update_page.php文件中的库存".我希望看到选择的处理中"值.

If for example I input a data with "In Process" and later I would like to update that perticular data. The default back to "In Inventory" on my update_page.php file. I would like to see the "In Process" value selected.

这是我update_page.php中无效代码的摘录:

Here is a snippet from my non-working code from my update_page.php:

<select name="status" value="<?php echo $row['status']; ?>">
    <option>In Inventory</option>
    <option>In Process</option>
    <option>Shipped/in-transit</option>
    <option>Received by recipient</option>
</select>

推荐答案

在HTML中选择没有值属性-您可以选择多个选项,这取决于选项元素上的所选属性

Select has no value attribute in HTML - you can have multiple options selected, and this is determined by the selected attribute on the option element

糟糕的方式:

<select name="status">
    <option<?php if ($row['status'] == "In Inventory"): ?> selected="selected"<?php endif; ?>>In Inventory</option>
    <option<?php if ($row['status'] == "In Process"): ?> selected="selected"<?php endif; ?>>In Process</option>
    <option<?php if ($row['status'] == "Shipped/in-transit"): ?> selected="selected"<?php endif; ?>>Shipped/in-transit</option>
    <option<?php if ($row['status'] == "Received by recipient"): ?> selected="selected"<?php endif; ?>>Received by recipient</option>
</select>

好一点的方法:

 <?php
      $options = array("In Inventory", "In Process", "Shipped/in-transit", "Received by recipient");
 ?>

 <select>
     <?php foreach ($options as $option): ?>
         <option value="<?php echo $option; ?>"<?php if ($row['status'] == $option): ?> selected="selected"<?php endif; ?>>
             <?php echo $option; ?>
         </option>
     <?php endforeach; ?>
 </select>

最好的方法-将这些选项存储在数据库表中-可能更好地使用ID值而不是字符串(允许更轻松地更新选项标签),并像我上面那样遍历从数据库查询中获取的可能选项.如果此选择列表使用率很高,请缓存查询结果以获取选项(请记住,如果列表已更新,则需要清除缓存)

Best way - store these options in a database table - probably better using ID values rather than strings (allows for easier updating of option labels) and loop over the possible options taking from a DB query like I have above. If this select list is used a lot, cache the results of the query to get the options (remember that you'd need to clear the cache if the list gets updated)

这篇关于HTML&lt; Select&gt; &lt; Option&gt;默认基于MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆