如何使用php和mysql简化搜索和过滤条件

时间:2017-08-09 12:15:21

标签: php mysql

我正在开发一个电子商务Web应用程序。在这个使用过滤器选项来搜索各种产品。

我使用的过滤器选项是store, price, discount, color, size

因为我使用if else来创建条件并在数据库中搜索它。

我的代码是,

    if($_GET["cid"] != null or $_GET["size"] != null or $_GET["dis"] != null or $_GET["prf"] != null or $_GET["prt"] != null or $_GET["store"] != null or $_GET["cat"] != null or $_GET["sub"] != null or $_GET["brand"] != null) {
        $query = null;
        $query_w = null;
        $query .= 'SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id';

        if($_GET["cat"] != null)
            $query_w .= ' a.product_sec_category_id = "'.$_GET["cat"].'"';
        if($_GET["sub"] != null)
            $query_w .= ' a.product_subcategory_id = "'.$_GET["sub"].'"';
        if($_GET["brand"] != null)
            $query_w .= ' a.product_brand_id = "'.$brand.'"';
        if($_GET["cid"] != null) {
            $query .= ' INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN tbl_product_color pc ON pc.product_color_id = ac.product_color_id';
            $query_w .= ' AND ac.product_color_id = "'.$_GET["cid"].'"';
        }
        if($_GET["size"] != null) {
            $query .= ' INNER JOIN tbl_fa_api_size ac ON ac.api_product_id = a.api_product_id';
            $query_w .= ' AND ac.api_size_size = "'.$_GET["size"].'"';
        }
        if($_GET["dis"] != null)
            $query_w .= ' AND (a.api_discount BETWEEN "'.$_GET["dis"].'" AND "99")';
        if($_GET["prf"] != null and $_GET["prt"] != null)
            $query_w .= ' AND (a.api_retail BETWEEN "'.$_GET["prf"].'" AND "'.$_GET["prt"].'")';
        if($_GET["store"] != null) {
            if($_GET["store"] == 'a')
                $query_w .= ' AND a.api_type = "One"';
            else if($_GET["store"] == 'f')
                $query_w .= ' AND a.api_type = "Two"';
            else if($_GET["store"] == 'af' or $_GET["store"] == 'fa')
                $query_w .= ' AND (a.api_type = "One" OR a.api_type = "Two")';
        }

        $query_w .= ' AND a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6';

        echo $query. " WHERE".$query_w;
    }

$_GET["cid"]color$_GET["dis"]discount$_GET["prf"]price from$_GET["prt"]price to$_GET["cat"]category$_GET["sub"]subcategory

在上面的代码中使用连接来获取mysql query。但我的代码不正确。 mysql query is wrong

例如,

$_GET["cid"] = 1$_GET["dis"] = 12当前mysql query

SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN tbl_product_color pc ON pc.product_color_id = ac.product_color_id WHERE and ac.product_color_id = "1" AND (a.api_discount BETWEEN "12" AND "99") AND a.api_status = 1 ORDER BY a.api_id DESC limit 0, 6

上面的代码错误。

如何使用连接制作正确的mysql query

还是有办法简化我的if else condition。我被困在这里。三江源。

1 个答案:

答案 0 :(得分:2)

你可以这样做。但是,我们建议您PDO或mysqli更安全地运行此查询。

if($_GET["cid"] != null or $_GET["size"] != null or $_GET["dis"] != null or $_GET["prf"] != null or $_GET["prt"] != null or $_GET["store"] != null or $_GET["cat"] != null or $_GET["sub"] != null or $_GET["brand"] != null) {
$condition = array();$query_w = '';
$query = 'SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id';

if($_GET["cat"] != null)
    $condition[] = 'a.product_sec_category_id = "'.$_GET["cat"].'"';
if($_GET["sub"] != null)
    $condition[] = 'a.product_subcategory_id = "'.$_GET["sub"].'"';
if($_GET["brand"] != null)
    $condition[] = 'a.product_brand_id = "'.$brand.'"';
if($_GET["cid"] != null) {
    $query .= ' INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN sh17n_product_color pc ON pc.product_color_id = ac.product_color_id';
    $condition[] = 'ac.product_color_id = "'.$_GET["cid"].'"';
}
if($_GET["size"] != null) {
    $query .= ' INNER JOIN tbl_fa_api_size ac ON ac.api_product_id = a.api_product_id';
    $condition[] = 'ac.api_size_size = "'.$_GET["size"].'"';
}
if($_GET["dis"] != null)
    $condition[] = '(a.api_discount BETWEEN "'.$_GET["dis"].'" AND "99")';
if($_GET["prf"] != null and $_GET["prt"] != null)
    $condition[] = '(a.api_retail BETWEEN "'.$_GET["prf"].'" AND "'.$_GET["prt"].'")';
if($_GET["store"] != null) {
    if($_GET["store"] == 'a')
        $condition[] = 'a.api_type = "One"';
    else if($_GET["store"] == 'f')
        $condition[] = 'a.api_type = "Two"';
    else if($_GET["store"] == 'af' or $_GET["store"] == 'fa')
        $condition[] = '(a.api_type = "One" OR a.api_type = "Two")';
}

if(!empty($condition)){
    $query_w = implode(' AND ', $condition);
}

if($query_w != '')
    $query_w .= ' AND a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6';
else
    $query_w  = 'a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6';

echo $query. " WHERE ".$query_w;
}