I have a MySQL database with about 40 rows in it. I also I have a form with <select> menu where a user can make a selection, click update and it updates what that user selects in a column in the database. Then the user is able to come back to the form at a later date and change what they previously selected. The contents of the select menu are pulled from the DB in alphabetical order. What I want to do is change the code for the <select> menu so that it lists all of the options from the database and the one that is selected in the form is the one that the user previously selected and updated.
The following works, but it lists what the user has previously selected at the top of the list and that option is repeated further down the list where it is alphabetically. Any ideas on how to eliminate this duplication and have the users previous selection selected, but have that selection remain in alphabetical order?
Thanks in advance!!!
<?php
$sql = "SELECT upc, artist_name, album_title, date_format(release_date, '%Y-%m-%d') as release_date, date_format(add_date, '%M %d, %Y') as add_date, description, track_list, price, image, also_avail_top, also_avail_mid, also_avail_bot FROM lounge_products WHERE upc = $upc";
$rows = $db->getRow($sql);
$also_avail_mid = $rows[also_avail_mid];
$sql_mid = "SELECT upc, artist_name, album_title FROM lounge_products WHERE upc = $also_avail_mid";
$rows_mid = $db->getRow($sql_mid);
$artist_name_mid = stripslashes($rows_mid[artist_name]);
$album_title_mid = stripslashes($rows_mid[album_title]);
print "<select name=\"middle\">";
print "<option value=\"0\"></option>";
if ($also_avail_mid == 0){
print "<option value=\"0\" selected></option>";
} else {
print "<option value=\"$rows_mid[upc]\" selected>$artist_name_mid - $album_title_mid</option>";
}
$rows = $db->getAll('SELECT upc, artist_name, album_title, date_format(release_date, \'%M %d, %Y\') as release_date, date_format(add_date, \'%M %d, %Y\') as add_date, description, price, image FROM lounge_products');
foreach ($rows as $row) {
$artist_name = stripslashes($row[artist_name]);
$album_title = stripslashes($row[album_title]);
print "<option value=\"$row[upc]\">$artist_name - $album_title</option>";
}
print "</select>";
?>