A long time ago for a school project I created a website for a small business owned by a friend of the family. As I've learned new things I wanted to improve the code (since before it was god awful ugly) I have a admin page for a website where the owner can edit each inventory item individually. It's a car dealership so there are lots of drop-down options for make, model, color, etc. Instead of having something like this to generate a selected item in the drop-down menu
echo "<tr>\n";
echo "<td class='viewLefthead'><strong>Body Style:</strong></td>\n";
echo "<td class='viewRighthead2'><select name='bodytype'>\n";
echo "<option value='Convertible'";
if($myrow['bodytype']=="Convertible") { echo " selected='selected'"; }
echo ">Convertible</option>\n";
echo "<option value='Coupe'";
if($myrow['bodytype']=="Coupe") { echo " selected='selected'"; }
echo ">Coupe</option>\n";
echo "<option value='Hatchback'";
if($myrow['bodytype']=="Hatchback") { echo " selected='selected'"; }
echo ">Hatchback</option>\n";
echo "<option value='Sedan'";
if($myrow['bodytype']=="Sedan") { echo " selected='selected'"; }
echo ">Sedan</option>\n";
echo "<option value='SUV'";
if($myrow['bodytype']=="SUV") { echo " selected='selected'"; }
echo ">SUV</option>\n";
echo "<option value='Truck'";
if($myrow['bodytype']=="Truck") { echo " selected='selected'"; }
echo ">Truck</option>\n";
echo "<option value='Van/Minivan'";
if($myrow['bodytype']=="Van/Minivan") { echo " selected='selected'"; }
echo ">Van/Minivan</option>\n";
echo "<option value='Wagon'";
if($myrow['bodytype']=="Wagon") { echo " selected='selected'"; }
echo ">Wagon</option>\n";
echo "</select></td>\n";
echo "</tr>\n";
I call to a function which grabs the options from an array.
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='viewLefthead'><strong>Body Style:</strong></td>\n";
echo "<td class='viewRighthead2'>";
createDropdown($bodytype_options, 'bodytype', $bodytype);
echo "</td>\n";
echo "</tr>\n";
Which is set up like this
// INVENTORY ARRAYS
$color_options = array('Black', 'Blue', 'Bronze', 'Brown', 'Gold', 'Gray', 'Green', 'Purple', 'Red', 'Silver', 'Tan', 'White', 'Yellow');
$bodytype_options = array('Convertible', 'Coupe', 'Hatchback', 'Sedan', 'SUV', 'Truck', 'Van/Minivan', 'Wagon');
$make_options = array('Acura', 'Aston Martin', 'Audi', 'Bentley', 'BMW', 'Buick', 'Cadillac', 'Chevrolet', 'Chrysler', 'Dodge', 'Ferrari', 'Ford', 'Geo', 'GMC', 'Honda', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Jaguar', 'Jeep', 'Kia', 'Lamborghini', 'Land Rover', 'Lexus', 'Lincoln', 'Lotus', 'Maserati', 'Mazda', 'Mercedes-Benz', 'Mercury', 'MG', 'Mini', 'Mitsubishi', 'Nissan', 'Oldsmobile', 'Plymouth', 'Pontiac', 'Porsche', 'Ram', 'Rolls-Royce', 'Saab', 'Saturn', 'Scion', 'Smart', 'Subaru', 'Suzuki', 'Toyota', 'Volkswagen', 'Volvo');
function createDropdown($array, $name, $selected) {
echo "<select name=\"{$name}\">";
foreach ($array as $key => $value) {
$select = array_search($selected, $array);
$select .= " select='selected'";
echo "<option value=\"{$value}\"{$select}>{$value}</option>\n";
}
echo '</select>';
}
My problem is that even though all the options are properly displaying, all of them are also showing up as selected. For example, the most recent entry is a Sedan but on the edit page it shows Wagon and in the HTML all of the drop-down items have select="selected"