php HTML <Select> <Option> 默认基于 MySQL 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17076292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML <Select> <Option> default based on MySQL data
提问by hangee
I have a simple inventory database and on the input_data form, one of the input fields is field, as follow:
我有一个简单的库存数据库,在 input_data 表单上,输入字段之一是字段,如下所示:
<select>
<option>In Inventory</option>
<option>In Process</option>
<option>Shipped/in-transit</option>
<option>Received by recipient</option>
</select>
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 文件中默认返回“In Inventory”。我想看到选择的“处理中”值。
Here is a snippet from my non-working code from my update_page.php:
这是我的 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>
回答by HorusKol
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 在 HTML 中没有 value 属性 - 您可以选择多个选项,这由 option 元素上的 selected 属性决定
Mucky way:
糊涂方式:
<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>
Slightly better way:
稍微好一点的方法:
<?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>
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)
最好的方法 - 将这些选项存储在数据库表中 - 使用 ID 值而不是字符串可能更好(允许更容易地更新选项标签)并循环从数据库查询中获取的可能选项,就像我上面那样。如果这个选择列表被大量使用,缓存查询结果以获取选项(请记住,如果列表更新,您需要清除缓存)
回答by xmarston
Each option has its corresponding value not the select tag. So, you will have to compare the values of each option with the status.
每个选项都有其对应的值,而不是 select 标签。因此,您必须将每个选项的值与状态进行比较。
回答by Joel Hinz
You'll have to check for each one if it's the selected one, like this, for exampel:
您必须检查每一个是否是选定的,例如:
<option <?php if ($row['status'] == 'In Process') echo 'selected'; ?>>In Process</option>
If you use jQuery, you could do a somewhat cleaner solution, in your case give your select tag an id, then:
如果你使用 jQuery,你可以做一个更简洁的解决方案,在你的情况下给你的 select 标签一个 id,然后:
$(document).ready(function() {
$('#select-id option:contains("<?php echo $row['status']; ?>")').prop('selected', true);
});
回答by Sarchophagi
The simpler code I've found, using only JavaScript (no jQuery needed). Put it right after the declaration of the listbox and set the selected index there:
我发现的更简单的代码,只使用 JavaScript(不需要 jQuery)。把它放在列表框的声明之后,并在那里设置选定的索引:
<script>
document.getElementById('listBoxId').selectedIndex=<?php echo $INDEX ?>;
</script>