PHP设置选定的下拉框值 [英] PHP set selected value of dropdown box

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

问题描述

我有一个下拉框,我用PHP构建。以下是代码:

  $ region_result = mysql_query(SELECT * FROM region ORDER BY region); 

$ dropdown =< select name ='region'>;
while($ row = mysql_fetch_assoc($ region_result)){
$ rid = $ row [id];
$ region = $ row [region];

$ dropdown。=\r\\\
< option value ='{$ row ['rid']}'> {$ region}< / option>;
}
$ dropdown。=\r\\\
< / select>;

在处理上述代码之后,我需要设置下拉框的选定值。有没有办法做到这一点?



有没有人有任何建议?谢谢!



编辑:



感谢大家的答案。让我解释一下我在做什么我正在设置一个编辑用户页面,您可以通过多个条件搜索用户,然后将结果列在编辑模式中,即 - 在文本框和下拉框中。所以你可以编辑和更新一个用户。对于两个用户字段,我需要在下拉框中列出数据(以确保数据完整性和约束)。所以,我想显示所有可能改变的值的下拉框,但我希望下拉列表的选定值是当前与用户关联的值。



所以,我可以让这个工作与欺骗的建议 - 在我的while循环中,这是设置我的PHP值与数据库结果,我已经插入一个嵌套的while循环,它将构造$下拉列表,嵌套if循环。我对所有这些嵌套循环都不是很疯狂。以下是代码段:

  if(@mysql_num_rows($ result)){
while($ r = @ mysql_fetch_assoc($ result)){
$ fname = $ r [fname];
$ lname = $ r [lname];
$ region = $ r [region];
$ role = $ r [role];
$ extension = $ r [extension];
$ username = $ r [username];
$ building = $ r [building];
$ room = $ r [room];?>

<?php
$ dropdown =< select name ='region'>;
while($ row = mysql_fetch_assoc($ region_result)){
$ rid = $ row [id];
$ region2 = $ row [region];

if($ region == $ region2){
$ dropdown。=\r\\\
< option selected ='selected'value ='{$ row ['rid' ]}'> {$区域}< /选项>中;
} else {
$ dropdown。=\r\\\
< option value ='{$ row ['rid']}'> {$ region2}< / option>;
}
}
$ dropdown。=\r\\\
< / select>;
?>

但是,我正在考虑将其更改为文本替换(由soulscratch和zombat建议),因为我认为这样做会更好。



...当多个结果集符合搜索条件时,这似乎不起作用(作为下拉框



你们有什么想法?

解决方案

使用您的字符串构建方式,这是一个相当简单的 str_replace() ,这是很好的,因为它可以节省需要正则表达式的麻烦:

  $ dropdown = str_replace(value =$删除',value ='。$ rid。'selected = \selected\,$ dropdown); 


I have a dropdown box that I construct with PHP. Here is the code:

 $region_result = mysql_query("SELECT * FROM region ORDER BY region");    

 $dropdown = "<select name='region'>";
while($row = mysql_fetch_assoc($region_result)) {
    $rid = $row["id"];
    $region = $row["region"];

    $dropdown .= "\r\n<option value='{$row['rid']}'>{$region}</option>";
}
 $dropdown .= "\r\n</select>";

I need to set the selected value of the dropdown box AFTER the above code is processed. Is there any easy way to do this?

Does anyone have any suggestions? Thanks!

EDIT:

Thank you all for your answers. Let me explain what I am doing. I was setting up an "Edit Users" page, where you can search for a user by multiple criteria and then the results are listed in an "edit mode" - that is - in text boxes and dropdown boxes. So you can then edit and update a user. For two user fields, I need to list the data in dropdown boxes (to ensure data integrity and constraints). So, I want to show those dropdown boxes with all the possible values you can change to, except I want the selected value of the dropdown to be the one currently associated with the user.

So, I was able to get this working with deceze's suggestion - In my while loop that has that is setting my PHP values with the database results, I have inserted a nested while loop which will construct $dropdown, and within that, a nested if-loop. I'm not crazy about all these nested loops. Here is the code segment for that:

 if (@mysql_num_rows($result)) {
        while ($r=@mysql_fetch_assoc($result)) {    
            $fname = $r["fname"];
            $lname = $r["lname"];
            $region = $r["region"];
            $role = $r["role"];
            $extension = $r["extension"];
            $username = $r["username"];
            $building = $r["building"];
            $room = $r["room"];?>

            <?php
            $dropdown = "<select name='region'>";
            while($row = mysql_fetch_assoc($region_result)) {
                $rid = $row["id"];
                $region2 = $row["region"];

                if($region == $region2){
                    $dropdown .= "\r\n<option selected='selected' value='{$row['rid']}'>{$region}</option>";
                }else{
                    $dropdown .= "\r\n<option value='{$row['rid']}'>{$region2}</option>";
                }
            }
            $dropdown .= "\r\n</select>";
            ?>

However, I am considering changing this to the text replacement (suggested by soulscratch and zombat), as I think it would be better on performance.

...This doesn't seem to work when more than one result set meets the search criteria, though (as the dropdown boxes for the 2nd and 3rd and etc. results are empty).

What do you guys think?

解决方案

With the way your string is built, it's a fairly simple str_replace(), which is nice as it saves the hassle of needing regular expressions:

$dropdown = str_replace("value='".$rid."'","value='".$rid."' selected=\"selected\"",$dropdown);

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

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