html - PHP set selected value of dropdown box -


I have a dropdown box that I'm building with PHP. Here's the code:

  $ region_result = mysql_query ("select area from area") ;; $ Dropdown = "& lt; select name = 'region' & gt;"; While ($ line = mysql_fetch_assoc ($ region_result)) {$ rid = $ row ["id"]; $ Area = $ line ["area"]; $ Dropdown. = "\ R \ n & lt; option value = '{$ line [' rid ']}' '{$ region}' / option & gt;"; } $ Dropdown. = "\ R \ n & lt; / select & gt;";  

After the above code has been processed, I have to set the selected value of the dropdown box. Is there any easy way to do this?

Do anyone have any suggestions? Thanks!

EDIT:

Thank you for your reply. Tell me what I'm doing. I was setting up a "edit user" page, where you can search the user with many criteria and then the results are listed in "edit mode" - that is - in the text boxes and dropdown boxes, then you edit a user Can update more For two user regions, I have to list the data in the dropdown box (to ensure data integrity and constraints) Therefore, I want to show the drop down box with all the possible values ​​that you can change, except for the dropdown The currently selected value is currently associated with the user.

So, I was able to get this work with the suggestion of fraud - in my loop that is setting my PHP value with the results of the database, I have put a nested while loop Which will create a $ dropdown, and within it, nested if-loop I am not mad about all these nested loops, the code segment for this is:

  if (@ mysql_num_rows ($ result)) {while ($ r = @ mysql_fetch_assoc ($ result)) { $ Fname = $ r ["fname"]; $ Lname = $ r ["lname"]; $ Area = $ r ["area"]; $ Role = $ r ["roles"]; $ Extension = $ r ["extension"]; $ Username = $ r ["username"]; $ Building = $ r ["building"]; $ Room = $ r ["room"] ;? & Gt; & Lt ;? Php $ dropdown = "& lt; select name = 'field' & gt;"; While ($ line = mysql_fetch_assoc ($ region_result)) {$ rid = $ row ["id"]; $ Area2 = $ line ["area"]; If ($ area == $ area2) {$ dropdown = "\ R \ n & lt; option selected = 'selected' value = '{$ row [' rid ''}} '& gt; {$ region} & lt; / option & gt;"; } And {$ dropdown. = "\ R \ n & lt; option value = '{$ row [' rid ''}} '& gt; {$ region2} & lt; / option & gt;"; }} $ Dropdown. = "\ R \ n & lt; / select & gt;"; ? & Gt;  

However, I'm considering changing it to text replacement (suggested by spirits and zombate), because I think it would be better on performance.

... It does not seem that when more than one result set meets the search criteria, though (2 and 3 dropdown boxes and etc results are empty).

What do you guys think?

The way your string is created, it is quite easy, which is good because this problem Regular expression is required:

  $ dropdown = str_replace ("value = '". $ Raid. "", "Value ='". $ Get rid of. "'Selected = \ "Selected \", $ dropdown);  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -