Jump to content

[SOLVED] pull default value from mysql with drop downs


johntp

Recommended Posts

I have a dropdown menu, and i am trying to get it to set the selected option as whatever the user selected and stored into the database. so say we have 3 options test, blah , and crap. If they chose test it would save to the database, and then when they edit their info i call it and have it as the selected dropdown, but they can change options in the dropdown. I know how to call it, and display it but i dont know how to set it as the default options

 

                                      <select name="Location">
                                               <option value="test">test</option>
                                               <option value="blah">blah</option>
                                               <option value="crap" selected>crap</option>
                                               </select>                                    

 

Is there a really easy way to do this? I tried If else statements and they dont work, and i messed around with a switch but not really sure how to do those. Any help would be appreciated.

I kinda figured it out. It's alot fater than i had hoped but if anyone else needs this answer here you go.

 

<?php 

$dropDownList = array ( 
                    "test", 
                    "blah", 
                    "crap"
                ); 

$result = mysql_query("SELECT `Section` FROM `News` WHERE `RECNO`='$RECNO';"); 
$dataArray = mysql_fetch_array($result); 
echo "<select name=\"status\">\n"; 

foreach($dropDownList as $listItem) 
{ 
    if ($dataArray[0] == $listItem) 
    { 
        echo "<option value=\"{$listItem}\" SELECTED>{$listItem}</option>\n"; 
    } 
    else 
    { 
        echo "<option value=\"{$listItem}\">{$listItem}</option>\n"; 
    } 
} 

echo "</select>"; 
?>

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.