0

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"

3 Answers 3

1

This should get you working good.

/* Get HTML select dropdown from array data */
function generateDropdown($name, $data, $key_column, $value_column, $selector_text = null, $args = array(), $selected_key = null){
    if(!($name && $data && $key_column && $value_column)){return "Invalid params";}

    $select_args = '';
    if(count($args)>0){
        foreach($args as $k=>$v){
            $select_args .= ' '.$k;
            if($v!='') $select_args .= '='.'"'.$v.'" ';
        }
    }
    $html = "<select name=".$name." ".$select_args.">";
    if($selector_text)
        $html .= "<option value='' disabled>".$selector_text."</option>";
    foreach($data as $k=>$v){
        $html .= "<option value='".$v[$key_column]."' ".($selected_key == $v[$key_column] ? 'selected':'') .">".$v[$value_column]."</option>";
    }
    $html .= "</select>";
    return $html;
}
Sign up to request clarification or add additional context in comments.

Comments

0

That's because you're applying it to all of them without condition.

if($value==$selected) {
    $select .= " select='selected'";
}

Comments

0

It looks like you really just need to compare $selected to $value to determine whether or not to set the selected property:

   function createDropdown($array, $name, $selected) {
      echo "<select name=\"{$name}\">";
      foreach ($array as $key => $value) {
         // Test if `$selected == $value`
         if ($selected == $value) {
             $select = " selected='selected'";
         }
         else $select = "";
         echo "<option value=\"{$value}\"{$select}>{$value}</option>\n";
      }
      echo '</select>';
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.