調用此腳本的頁面允許用戶從1到5個字段中選擇。這個想法是將所選字段的數量相加並獲取適當的結果。無論從表單中選擇多少個字段,if/elseif語句都不起作用,我看不到問題。 任何幫助將不勝感激。php if/elseif不工作
$sql = "SELECT * FROM my_table";
$result = $conn->query($sql);
$fields_select = 0;
if($country_type != "") {
$fields_select = $fields_select + 1;
$winearr[] = "\$country_type";
$winearr[] = "Country";
}
if($region_type != "") {
$fields_select = $fields_select + 1;
$winearr[] = "\$region_type";
$winearr[] = "Region";
}
if($wine_type != "") {
$fields_select = $fields_select + 1;
$winearr[] = "\$wine_type";
$winearr[] = "Type";
}
if($rating_type != "") {
$fields_select = $fields_select + 1;
$winearr[] = "\$rating_type";
$winearr[] = "Rating";
}
if($vintage_type != "") {
$fields_select = $fields_select + 1;
$winearr[] = "\$vintage_type";
$winearr[] = "Vintage";
}
if ($result->num_rows > 0) {
if ($fields_select == 0) {
echo "No Results Found In Search";
} elseif ($fields_select == 1) {
while($row = $result->fetch_assoc()) {
if($winearr[0] == $row[$winearr[1]]) {
makeListing($row); // call the function
}
}
} elseif ($fields_select == 2) {
while($row = $result->fetch_assoc()) {
if($winearr[0] == $row[$winearr[1]] && $winearr[2] == $row[$winearr[3]]) {
makeListing($row); // call the function
}
}
} elseif ($fields_select == 3) {
while($row = $result->fetch_assoc()) {
if($winearr[0] == $row[$winearr[1]] && $winearr[2] == $row[$winearr[3]] && $winearr[4] == $row[$winearr[5]]) {
makeListing($row); // call the function
}
}
} elseif ($fields_select == 4) {
while($row = $result->fetch_assoc()) {
if($winearr[0] == $row[$winearr[1]] && $winearr[2] == $row[$winearr[3]] && $winearr[4] == $row[$winearr[5]] && $winearr[6] == $row[$winearr[7]]) {
makeListing($row); // call the function
}
}
} elseif ($fields_select == 5) {
while($row = $result->fetch_assoc()) {
if($winearr[0] == $row[$winearr[1]] && $winearr[2] == $row[$winearr[3]] && $winearr[4] == $row[$winearr[5]] && $winearr[6] == $row[$winearr[7]] && $winearr[8] == $row[$winearr[9]]) {
makeListing($row); // call the function
}
}
}
}
/* Debug */
echo $winearr[0]."-".$winearr[1]."-".$winearr[2]."-".$winearr[3]."-".$winearr[4]."-".$winearr[5]."-".$winearr[6]."-".$winearr[7]."-".$winearr[8]."-".$winearr[9];
echo "<br>\$fields_select=".$fields_select;
你有沒有做任何調試? '$ fields_select'的價值是什麼? –