Jump to content

[SOLVED] Automate Selected Value in Drop Down List


mfallon

Recommended Posts

Hi,

 

I have a page which updates a value in my DB for a record.

 

The value is selectable from a drop down list however I have no way of defaulting the drop down list to the current value in the DB.

 

Can anyone tell me if this is possible, and if so how.

 

</table>
              <tr>
                <td align="right"><strong>Current Status: </strong></td>
                <td><? echo $req_sid_info['stype']; ?></td>
              </tr>
              <tr>
                <td align="right"><strong>New Status: </strong></td>
                <td><select name="Status" id="Status">
                  <option value="1" selected="selected">Verified</option>
                  <option value="0">Unverified</option>
                </select></td>
              </tr>
</table>

 

The list I have provided is just a basic one. I guess I could do it by using an if value = 0{ else value = 1{ but I would rather not do this as I have a couple of very large drop down lists and it would make it much more complex.

 

Thanks in advance for the help.

 

Matt

in your current syntax i think the only way to do it would be to have an IF statement check the current value with the value in the database and if it matches, echo "selected=selected"

 

the quicker way to change it would be to have all of the possible options in an array and then a foreach loop to go through the array and check each one to see if it matches

Here's the code I use, as an example I'll populate a list of countries...

$htmCountries='';
$query=mysql_query("SELECT id,country FROM countries ORDER BY country ASC");
while ($row=mysql_fetch_assoc($query)) {
  $htmCountries.='<option value="'.$row['id'].'"'.($row['id']==$_POST['country'] ? ' selected="selected"' : '').'>'.$row['country'].'</option>';
}

 

Then to display it...

Country: <select name="country"><?php echo $htmCountries; ?></select>

 

This bit:

$row['id']==$_POST['country'] ? ' selected="selected"' : ''

if exactly the same as this:

if ($row['id']==$_POST['country']) {
  $htmCountries.=' selected="selected"';
}

edit: similar as other posts...but i'm posting anyways :)

 

the easiest way is to get the choices into an array (either manually defining the array or via info from a db)...then looping over the values and testing:

 

<?php
  $list = array(
    '1' => 'Verified',
    '0' => 'Unverified',
  );
  print "<select name=\"Status\" id=\"Status\">\n";
  foreach($list as $key => $val){
    $selected = ($req_sid_info['status'] == $key) ? ' selected="selected"' : '';
    print "  <option value=\"{$key}\"{$selected}>{$val}</option>\n";
  }
  print "</select>\n";
?>

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.