I need to create filter function for this 3 information: Category, Price and Discount for users in order to filter either 1 of the information or any of the 2 information together, or all 3 information together.
I used this method to filter single information and it successfully displayed the results, but when I tried to use this method to filter any of the 2 or 3 information together, it failed to filter everything.
<?php
//filter category only
if (isset($_GET['f_category']) && $_GET['f_category'] != ""){
$f_category = $_GET['f_category'];
$sql = "SELECT * FROM post_ads WHERE sup_category='$f_category'";
}
//filter price only
if (isset($_GET['min_price']) && $_GET['min_price'] != "" && $_GET['max_price'] && $_GET['max_price'] != "") {
$min_price = $_GET['min_price'];
$max_price = $_GET['max_price'];
$sql = "SELECT * FROM post_ads WHERE sup_price>='$min_price' AND sup_price<='$max_price'";
}
// filter discount only
if (isset($_GET['f_discount']) && $_GET['f_discount'] != ""){
$f_discount = $_GET['f_discount'];
$sql = "SELECT * FROM post_ads WHERE sup_discount='$f_discount'";
}
//filter category and price
if (isset($_GET['f_category']) && $_GET['f_category'] != "" || $_GET['min_price'] && $_GET['min_price'] != "" && $_GET['max_price'] && $_GET['max_price'] != ""){
$f_category = $_GET['f_category'];
$min_price = $_GET['min_price'];
$max_price = $_GET['max_price'];
$sql = "SELECT * FROM post_ads WHERE sup_category='$f_category'
AND sup_price>='$min_price' AND sup_price<='$max_price'";
}
//filter category and discount
if (isset($_GET['f_category']) && $_GET['f_category'] != "" || $_GET['f_discount']
&& $_GET['f_discount'] != ""){
$f_category = $_GET['f_category'];
$f_discount = $_GET['f_discount'];
$sql = "SELECT * FROM post_ads WHERE sup_category='$f_category'
AND sup_discount='$f_discount'";
}
if(isset($sql)){
$result = mysql_query($sql, $con1);
while($rows=mysql_fetch_array($result))
{
//display results
}
}?>
Can I know what is the problem and how do I fix it?