...

View Full Version : Where clause, with if statement



habester
05-11-2009, 02:45 AM
Hi everyone, this is the first time that I've done much with PHP
My question is how would I include an IF statement within a WHERE clause, so that I can ignore the item if its submitted value is null? $datemonthF and $datedayF are the submitted values.
Here's what I have now:

$query .= "WHERE datemonth = '". $datemonthF ."' and dateday = '". $datedayF ."'";
I'd like to negate each one that has the value null. I could also make it so they are negated if the value is nothing, "", whichever is easier. Thanks for any help!

oesxyl
05-11-2009, 03:09 AM
Hi everyone, this is the first time that I've done much with PHP
My question is how would I include an IF statement within a WHERE clause, so that I can ignore the item if its submitted value is null? $datemonthF and $datedayF are the submitted values.
Here's what I have now:

$query .= "WHERE datemonth = '". $datemonthF ."' and dateday = '". $datedayF ."'";
I'd like to negate each one that has the value null. I could also make it so they are negated if the value is nothing, "", whichever is easier. Thanks for any help!


$c1 = isset($datemonthF) && !empty($datemonthF);
$c2 = isset($datedayF) && !empty($datedayF);
if($c1 || $c2){
$query .= "WHERE";
if($c1){
$query .= " datemonth = '". $datemonthF ."'";
}
if($c1 && $c2){
$query .= " and ";
}
if($c2){
$query .= " dateday = '". $datedayF ."'";
}
}


best regards

habester
05-11-2009, 03:53 AM
perfect! thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum