tags:

views:

5079

answers:

5

Hey everyone,

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?

+1  A: 

If you want to change your assembled HTML after the fact you need to use complicated string replace methods, or Javascript, neither of which is a good choice.

The best option you have would be to restructure your program so you can set the selected attribute when going through the loop the first time around.

deceze
Thanks a lot,I was able to restructure my code, but I know have several levels of nested loops...(see Edit)
behrk2
A: 

You will be able to find it in $_REQUEST['region']

Ben Shelock
+4  A: 

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);
zombat
Zombat-This is not what I currently have implemented (see my Edit), but I'm thinking about changing my implementation to your suggestion...
behrk2
In his defense, this is _exactly_ what you asked for... a way to do it _after_ the loop.
jason
Got this way working. A lot better. Thanks!
behrk2
A: 

This was very helpfull to me... :)

dglite
A: 

I'll bet you'll run into this problem again, which means it'd be worthwhile to come up with a more flexible solution. Here's an idea toward that end:

First, use an array to hold options for your drop-down list. If you need multiple select elements with the same options, you get to re-use the array for free:

$options = array ();
while ($row = mysql_fetch_assoc($region_result)) {
  $options[$row['id']] = $row['region'];
}

Then, you can feed this into a function that generates a select control:

function getSelect ($name, $options, $current)
{
  $markup = '<select name="' . htmlspecialchars($name) . '">';
  foreach ($options as $value => $label)
  {
    $selected = ($value == $current) ? ' selected="selected"' : '';
    $markup .= sprintf(
      "<option value=\"%s\"%s>%s</option>\r\n",
      htmlspecialchars($value), 
      $selected, 
      htmlspecialchars($label)
    );
  }
  $markup .= '</select>';
  return $markup;
}

(You can also add one or more optional parameters to set the id, class, etc.)

Also, you mentioned you were considering switching methods because of speed, but it's very unlikely that this is the time and place to worry about performance. Focus on maintainable code now, and any performance tweaks that become necessary later will be easier and more effective.

grossvogel