Replies Back to Article
Populate a Dropdown list with MySQL data and select options in a edit page
This is a request for a tutorial. Hopefully someone will write it or provide a link to such a tutorial.
Martha Graham
DMXzone.com
You have to look at come code, but this shouldn't be TOO scary.
What you're trying to get is HTML that looks like this:
<select name="ship_type">
<option value="2" >Next Day</option>
<option value="1" >Fedex</option>
<option value="3" SELECTED>UPS Ground</option>
</select>
The first thing to do is create a recordset with your value and display fields. On the Application->Bindings window press the '+' sign and choose Recordset. If the "Advanced..." button is showing, click it to switch to Advanced view. Give your recordset a name - I used "rsShipType". In the SQL text box, type something like:
SELECT ship_type_id, ship_type_name
FROM ship_type
ORDER BY ship_type_name
Or, you can use the buttons at the bottom of the dialog to build your SQL query. Click Test to verify your code, and press OK to save it when you're happy.
Next, you want to modify the snippet of code below and paste it into the Code View of your page where the dropdown should be. I usually type some junk characters in the design view ('yyy', for example), select them, then switch to code view. Then paste your code over the selected characters.
Here's the code:
<select name="ship_type">
<?php do { ?>
<option value="<?php echo $row_rsShipType['ship_type_id']?>"
<?php if (!(strcmp($row_rsShipType['ship_type_id'],
$row_rsOrder['ship_type_id']))) {echo "SELECTED";} ?>>
<?php echo $row_rsShipType['ship_type_name']?>
</option>
<?php } while ($row_rsShipType = mysql_fetch_assoc($rsShipType)); ?>
</select>
In the code above, search for "rsShipType" and replace it with your recordset name. Replace "ship_type_id" with your identifier column and "ship_type_name" with your display column name. I also have a recordset called rsOrder that I'm using to set the "SELECTED" row. You should change this as appropriate.
The PHP "do while" statement loops through the recordset and adds an "option" line for each row.
I've searched for a long time trying to find something that worked. This information, and information that I found at a forum solved my problem.
Through Dreamweaver, I was able to create a record set using this information, but since the table would be updated with people's names, I could only have their name show just once in the drop down - names that were gotten from the mySQL database. But, what if someone submitted records multiple times? I didn't want their name added to the drop down list repeatedly, and only wanted new names added.
Adding unique names was found at: http://forums.devshed.com/archive/t-46133