I have to make option to download report in excel where user will enter "from date", "to date" and "market name". Based on that data will be fetched from the database. All the 3 fields are not mandatory, so if user will enter any value then it will fetch data based on the value otherwise it will fetch all the data. Just for 3 fields I have to write 8 combinations of where clause, so my question is that if there are more input value from user end, then I have to write many combinations of where clause, sometimes even more than 20 combinations. Is there any easy and efficient way to do this.
<?php
if($date_from != NULL && $date_to != NULL && $market != NULL){
$whereCondition = "WHERE Date >= '$date_from' AND Date <= '$date_to' AND marketId = '$market' ";
}elseif($date_from == NULL && $date_to == NULL && $market == NULL){
$whereCondition = "WHERE 1";
}elseif($date_from != NULL && $date_to == NULL && $market != NULL){
$whereCondition = "WHERE Date >= '$date_from' AND marketId = '$market' ";
}elseif($date_from != NULL && $date_to == NULL && $market == NULL){
$whereCondition = "WHERE Date >= '$date_from' ";
}elseif($date_from == NULL && $date_to != NULL && $market != NULL){
$whereCondition = "WHERE Date <= '$date_to' AND marketId = '$market' ";
}elseif($date_from == NULL && $date_to != NULL && $market == NULL){
$whereCondition = "WHERE Date <= '$date_to'";
}elseif($date_from == NULL && $date_to == NULL && $market != NULL){
$whereCondition = "WHERE marketId = '$market' ";
}elseif($date_from != NULL && $date_to != NULL && $market == NULL){
$whereCondition = "WHERE Date >= '$date_from' AND Date <= '$date_to' ";
}
//finally my query will be below:
//MYSQL QUERY
$sql_data = "SELECT * FROM table_name $whereCondition ";