我有一个csv文件,可以创建一个选择选项列表。
提交所选选项后,清除。如何在选择中保留选项?
CSV文件:
"Agusta Westland 109s",A109,2,Turbine,IFR,7,155,4,250,2500
"Bell 427",B427,2,Turbine,VFR,7,140,5,200,2000
"Eurocopter AS355 Ecureuil",Squirrel,2,Turbine,VFR,5,120,5,180,1800
"Eurocopter EC130 B4",EC130,1,Turbine,VFR,6,120,4,140,2500
"Robinson R22",R22,1,Piston,VFR,1,90,8,50,300
"Robinson R44 Raven 2",R44,1,Piston,VFR,3,110,6,75,1100
PHP代码:
echo "<form method='post' action=''>";
if (!file_exists('types.csv')) {
exit("Your Types file doesn't exist");
} else {
$types = array_map('str_getcsv', file('types.csv'));
$aircraftTitles = array("Aircraft");
echo "<table class='table'>";
echo "<tr>";
foreach ($aircraftTitles as $row){
echo "<th>".$row."</th>";
}
echo "</tr>";
echo "<tr>";
//select type
echo "<td><select name = 'type'>";
echo "<option value='".null."'>Select Aircraft Type</option>";
foreach ($types as $row){
echo "<option value='".$row[1].",".$row[6].",".$row[7].",".$row[8]."'>".$row[1]."</option>";
}
echo "</select></td>";
echo "<tr>";
echo "</table>";
}
非常感谢您的帮助
答案 0 :(得分:0)
您需要跨数组的值运行$ _POST中的值。如果匹配,请选择所选选项。如果它不匹配,则只显示选项。
像这样:
echo "<form method='post' action=''>";
if (!file_exists('types.csv')) {
exit("Your Types file doesn't exist");
} else {
$types = array_map('str_getcsv', file('types.csv'));
$aircraftTitles = array("Aircraft");
echo "<table class='table'>";
echo "<tr>";
foreach ($aircraftTitles as $row){
echo "<th>".$row."</th>";
}
echo "</tr>";
echo "<tr>";
//select type
echo "<td><select name = 'type'>";
echo "<option value='" . null . "'>Select Aircraft Type</option>";
foreach($types as $row) {
$str = $row[1]. "," .$row[6] . "," . $row[7] . "," .$row[8];
if($_POST['type'] == $str) {
echo '<option value="' . $str . '"' . ' selected="selected"' . '>' . $row[1] . '</option>';
}else {
echo '<option value="' . $str . '">' . $row[1] . '</option>';
}
}
echo "</select></td>";
echo "<tr>";
echo "</table>";
}