PHP MySQL下拉框填充所选值 [英] PHP MySQL Drop Down Box Populate Selected Value

查看:100
本文介绍了PHP MySQL下拉框填充所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于如何使用MySQL填充整个下拉列表的教程,但我遇到的问题是我只想从数据库中获取这个列表,并将其作为选定的。所以我会像数据库中的三个项目(Item1,Item2,Item3)一样下降,它存储在一个名为itemschoice的列中,该列的值为'Item2'。

解决方案

在您的< option> 元素为 itemschoice 中的值添加所选的属性



使用补全功能获得选择的粗体示例:

  $ choice = get_items_choice(); 
$ results = mysqli_query($ sql);

echo'< select name =whatever>';
while($ row = mysqli_fetch_array($ results)){
if($ row ['choice'] === $ choice){
echo'< option value =' $ choice。'selected =selected/>';
} else {
echo'< option value ='。$ choice。'/>';
}
}
echo'< / select>';

这只是一个例子,不要复制&粘贴不添加某种错误验证!


I have read tutorials about how to populate an entire Drop down list with MySQL, but the problem I am running into is that I only want to grab the one from the database and have that be the selected one. So I would have like a drop down with three items (Item1, Item2, Item3) in the database its stored in a column called itemschoice which has a value of 'Item2'. How do I go about getting item2 to be selected when I load the drop down box?

解决方案

In your <option> element add the selected attribute for the value that is in itemschoice.

Crude example using a made up function to get the choice:

$choice = get_items_choice();
$results = mysqli_query($sql);

echo '<select name="whatever">';
while($row = mysqli_fetch_array($results)) {
    if ($row['choice'] === $choice) {
        echo '<option value="' . $choice . '" selected="selected" />';
    } else {
        echo '<option value="' . $choice . '" />';
    }
}
echo '</select>';

This is just an example, don't copy & paste this without adding some kind of error verification!

这篇关于PHP MySQL下拉框填充所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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