I need to make a query in MySQL.
But the query needs to be made dinamically with options choosen by the user.
The options are Day, Month, Year & SellerID.
A user will choose if he want to view the sales history from sellers by, days, months and years.
But, let’s say he wants to see all sales on March, 2012. He will leave Day in blank and receive all sales from March 01 till March 31…
And maybe he leaves the option Month in blank, and set Day=15 and Year=2012, he will get the sales history from the whole year 2012, and the 15th day of each month only.
My program needs to build a query that covers exactly what he wanted to view.
The question is:
Do I really need to use thousands of IF on my code? Isn’t there a better way to do that?
if( ($day==0) && ($month==0) && ($year>0) ){
$query.="`date`>='{$year}-01-01' AND `date`<='{$year}-12-31'";
}elseif( ($day==0) && ($month>0) && ($year>0) ){
$query.="`date`>='{$year}-{$month}-01' AND `date`<='{$year}-{$month}-31'";
}elseif{
etc...
You could preprocess the values before using them: