简体   繁体   English

php if / elseif根本不起作用

[英]php if/elseif not working at all

The page that calls this script allows the user to pick from 1 to 5 fields. 调用此脚本的页面允许用户从1到5个字段中进行选择。 The idea is to add up the number of fields selected and fetch the appropriate results. 想法是将选定字段的数量加起来并获取适当的结果。 No matter how many fields are selected from the form the if/elseif statements do not work and I cannot see the problem. 无论从窗体中选择了多少字段,if / elseif语句均不起作用,并且我看不到问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

$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;

From your code I gusset you are trying to search something by php which is not correct. 从您的代码我弄到您正在尝试通过php搜索不正确的内容。 Try to search by your query and let your database handle the search. 尝试通过查询进行搜索,然后让数据库处理搜索。

$sql = "SELECT * FROM my_table WHERE ";

if($country_type != "") {
    $sql .= 'Country = "' .  $country_type .'"';
}
if($region_type != "") {
    $sql .= 'AND Region = "' .  $region_type .'"';
}
.
.
.
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
            makeListing($row); // call the function
    }
}

in the end fetch your result. 最后获取您的结果。

Resolved - The problem is incorrect coding. 已解决 -问题是编码不正确。 $winearr[] = "\\$country_type"; $ winearr [] =“ \\ $ country_type”; returns $country_type instead of the variable that was selected. 返回$ country_type而不是所选变量。 Instead of $country_type being equal to Australia, is was literally $country_type every time no matter want was select. $ country_type并不是每次都等于澳大利亚,而是每次都选择$ country_type。

$winearr[] = "\$rating_type";

should have been 本来应该

$winearr[] = $rating_type;

The routine now works exactly as it should. 该例程现在完全可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM