I have the following problem: I want to let a user apply filters to a DB search. I have three filters, A, B and C. All of them can be \"empty\", as in, the user doesn\'t care a
Try this:
$db_q = "Select * from table ";
if ($A != "any" || $B != "any" || $C != "any")
{
$db_q .= "where ";
}
$firstCondition = true;
if ($A != "any")
{
if (!$firstCondition)
$db_q .= "and ";
$db_q .= "row1 = '$A' ";
$firstCondition = false;
}
if ($B != "any")
{
if (!$firstCondition)
$db_q .= "and ";
$db_q .= "row2 = '$B' ";
$firstCondition = false;
}
if ($C != "any")
{
if (!$firstCondition)
$db_q .= "and ";
$db_q .= "row3 = '$C' ";
$firstCondition = false;
}