【问题标题】:PHP if else statement. Search and Filtering using PHPPHP if else 语句。使用 PHP 进行搜索和过滤
【发布时间】:2021-01-06 13:49:53
【问题描述】:

问题是我无法到达第三个“else if 语句”,其中的场景是这样的:
我在 HTML 中有 3 个文本框 -> 最小值、最大值和搜索关键字。如果 2 个文本框有值,如果我将值放在 2 个文本框中,我想达到第 3 个,但发生的情况是它只返回到第一个 if 语句。

 <?php
    $price_min = $_POST['price_min'];
    $price_max = $_POST['price_max'];
    $keyword = $_POST['keyword'];
    $sql = 'SELECT * FROM products ';
    
    if (!empty($price_min)) {
      $sql .='WHERE price >='.$price_min;
    } else if (!empty($price_max)) {
      $sql .='WHERE price <='.$price_max;
    } else if (!empty($price_min) && !empty($price_max)) {
      $sql .='WHERE price BETWEEN '.$price_min.' AND '.$price_max;
    } else if (!empty($keyword) && !empty($price_min)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price >='.$price_min.')';
    } else if (!empty($keyword) && !empty($price_max)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price <='.$price_max.')';
    } else if (!empty($keyword) && (!empty($price_min) AND !empty($price_max)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price BETWEEN '.$price_min.' AND '.$price_max.')';
    } else {
      echo "all are empty// ";
    }
  ?>

【问题讨论】:

    标签: php html mysql if-statement select


    【解决方案1】:

    else if 仅在之前的if 条件为假时执行。一系列if/else if 将在第一个真实条件处停止。所以这应该只在条件互斥时使用。如果$price_min$price_max 都填写,第一个if 将成功,因此它不会执行任何else if 测试。您应该在自己检查其中任何一个之前测试 both

    iif (!empty($price_min) && !empty($price_max)) {
        $sql .='WHERE price BETWEEN '.$price_min.' AND '.$price_max;
    } else if (!empty($price_min)) {
      $sql .='WHERE price >='.$price_min;
    } else if (!empty($price_max)) {
      $sql .='WHERE price <='.$price_max;
    }
    

    但是,更好的方法是一次只检查一个变量,然后在最后收集所有 WHERE 条件。

    $conditions = [];
    
    if (!empty($price_min)) {
        $conditions[] = 'price >='.$conn->real_escape_string($price_min);
    }
    if (!empty($price_max)) {
        $conditions[] = 'price <='.$conn->real_escape_string($price_min);
    }
    if (!empty($keyword)) {
        $conditions[] = 'tags LIKE "%'.$conn->real_escape_string($keyword).'%"';
    }
    
    if (!empty($conditions)) {
        $sql .= 'WHERE ' . implode(' AND ', $conditions);
    }
    

    【讨论】:

    • 谢谢你!我认为您的代码比我的要好得多!但是如果“price_min and price_max”有值呢? “price >= $price_min AND price
    • 是的,a BETWEEN x AND y 等价于a &gt;= x AND a &lt;= y
    • 天哪,我被这个问题困扰了好几天,非常感谢!我可以再问一件事,如果你觉得可以吗?我忘了在我的问题中包含这个,我有这个用于类别过滤的复选框,问题是如果复选框只会选中 1,查询将是“category_id = $category”,但如果它是 2 或更多,查询将是“ category_id LIKE \'%'.$category_checked.'%\' OR category_id LIKE \'%'.$category_checked.'%\' and so on.. depends on how many category will be checked
    • 复选框应该在一个数组中。然后您可以遍历所有这些,并将它们收集到category_id LIKE "%' . $category_checked[$i] . '%" 的数组中,然后使用'(' . implode(' OR ', $categories) . ')' 组合它们。将其添加到您的 $conditions 数组中。
    • 不要在 cmets 中放长代码,它完全不可读。如果您有其他问题,请发布另一个问题,以便您可以正确格式化它l
    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 2012-09-19
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多