很久以前,我为一个学校项目创建了一个由家人朋友拥有的小企业网站。因为我已经学会了新的东西,我想改进代码(因为它之前很糟糕)我有一个网站的管理页面,所有者可以单独编辑每个库存项目。这是一个汽车经销商,所以有很多下拉选项,包括品牌,型号,颜色等。而不是像这样的东西在下拉菜单中生成一个选定的项目
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";
我调用了一个从数组中获取选项的函数。
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";
这样设置
// 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>';
}
我的问题是,即使所有选项都正确显示,所有选项也会显示为已选中。例如,最近的条目是轿车,但在编辑页面上显示Wagon,在HTML中所有下拉项目都有select =“selected”
答案 0 :(得分:1)
这可以让你工作得很好。
/* 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;
}
答案 1 :(得分:0)
那是因为你无条件地将它应用于所有这些。
if($value==$selected) {
$select .= " select='selected'";
}
答案 2 :(得分:0)
看起来您真的只需要将$selected
与$value
进行比较,以确定是否设置selected
属性:
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>';
}